Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 是否可以更改XAML中使用StaticResource元素实例化的资源的属性?_Wpf_Xaml - Fatal编程技术网

Wpf 是否可以更改XAML中使用StaticResource元素实例化的资源的属性?

Wpf 是否可以更改XAML中使用StaticResource元素实例化的资源的属性?,wpf,xaml,Wpf,Xaml,使用这片XAML: <Window.Resources> <Rectangle x:Key="rectangle" x:Shared="False" Width="20" Height="8" Fill="Red" /> </Window.Resources> <StaticResource x:Name="r1" ResourceKey="rectangle" /> <StaticResource x:Name="r2" Reso

使用这片XAML:

<Window.Resources>
    <Rectangle x:Key="rectangle" x:Shared="False" Width="20" Height="8" Fill="Red" />
</Window.Resources>

<StaticResource x:Name="r1" ResourceKey="rectangle" />
<StaticResource x:Name="r2" ResourceKey="rectangle" />
可以直接在XAML中执行吗?我试过:

<StaticResource ResourceKey="rectangle" Margin="3"/>
我想画除一个属性外,其他属性完全相同的矩形,例如边距或颜色

对我来说,这听起来像是一种风格。在WPF中,通常有几种不同的方法来完成相同的事情,这也不例外

设置视图模型似乎有点过大

也许吧。也许不是。很难说,因为你的问题中没有其他细节。也就是说,考虑到上面的问题陈述,我同意这肯定是没有必要的,而且如果没有任何其他要求,我看不到使用视图模型可以获得任何好处

尽管如此,下面的代码示例显示了三种不同的方式,其中两种基于视图模型和模板:


视图模型方法依赖于这些类(它们不实现
INotifyPropertyChanged
,因为在这个简单的示例中不需要):

类矩形视图模型
{
公共厚度边距{get;set;}
}
类MainViewModel
{
公共矩形视图模型矩形1{get;set;}
公共矩形视图模型矩形2{get;set;}
}
如您所见,在基于样式的方法中,可以使用资源字典中的单个
元素定义您喜欢的任何属性的默认值。然后,您可以在希望在窗口内容中显式使用
元素,显式设置任何其他属性。如果出于任何原因需要,您甚至可以替代在样式中设置的特性

请注意,在上面的示例中,数据模板显式设置其属性值。但实际上,您可以通过在声明模板时引用样式资源将这两种技术结合起来,如下所示:


正如注释中所指出的,在WPF中,将具体的UI元素声明为资源通常是错误的。我不敢说它总是错的,但我会说它几乎总是错的。具体的UI元素应该在XAML中声明为实际内容,使用样式为这些元素提供默认格式。否则,您应该使用模板,并允许WPF根据需要根据您提供的模板创建UI元素

当使用

简短回答:没有


StaticResource
标记扩展只引用基于键的资源。它无法更改已解析资源的任何属性。您必须设置已解析资源本身的属性,例如,将资源的目标属性强制转换为
矩形

“每个实例”都是一种误解。除非明确设置
x:Shared=“False”
,否则资源是共享的。因此r1和r2都引用同一个矩形实例。这就是为什么通常不将UIElements声明为资源的原因。可能吗?当然你试过什么?您不能设置资源对象本身,因为它不是共享的,因此会为您使用它的每个地方创建一个新实例。但您始终可以从代码隐藏中访问可视化树,并执行您想要的操作。你不应该这样。你似乎有一个好主意。更可能的情况是,您应该使用模板,并在视图模型中指定绑定到模板中属性的边距。@PeterDuniho:我已经尝试了
以及对象元素语法。当然,VS说
Margin
不是
StaticResource
的属性。我的需求是这样的:绘制具有相同属性的矩形,除了边距或颜色。所以我想定义一个资源。我也认为你应该在这里做模板。如果这些是画布中的矩形,则可以将viewmodels的observablecollection绑定到itemscontrol的itemssource。itemtemplate具有公共属性集和绑定到该viewmodel中属性的变量属性。但我只是在猜测你想要实现什么。可能只是设置所有公共属性的样式。您可能会使用默认矩形样式,该样式将自动应用于您在应用程序中实例化的所有矩形。感谢您花时间提供解决方案。目前,我已经使用了样式方法(并在问题中发布了我的代码,以便更准确地描述我的上下文)。这是令人满意和简单的。第一个问题呢?当使用
实例化资源时,XAML是否允许更改资源的属性?“XAML是否允许更改资源的属性…”——如果您询问是否严格在XAML中,您可以在
元素的使用点处修改其属性,那么:否。XAML不支持这一点。属性分配是通过XML属性进行的,没有对
有效的属性可以设置基础对象的属性值。
<StaticResource ResourceKey="rectangle" Margin="3"/>
<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Test"
        mc:Ignorable="d"

        Title="Transform Center" Height="400" Width="600">

    <Window.Resources>
        <Style x:Key="title" TargetType="TextBlock">
            <Setter Property="HorizontalAlignment" Value="Center" />
            <Setter Property="VerticalAlignment" Value="Bottom" />
            <Setter Property="Margin" Value="0,0,0,10" />
        </Style>
        <Style x:Key="rotated" TargetType="Rectangle">
            <Setter Property="Width" Value="201" />
            <Setter Property="Height" Value="81" />
            <Setter Property="Fill" Value="CadetBlue"/>
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>
        <Style x:Key="fixed" TargetType="Rectangle">
            <Setter Property="Width" Value="30" />
            <Setter Property="Height" Value="30" />
            <Setter Property="Fill" Value="Indigo" />
            <Setter Property="HorizontalAlignment" Value="Left"/>
        </Style>
    </Window.Resources>

    <Grid >
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <StackPanel Grid.Row="0" Grid.Column="0" Background="Beige" Margin="5">
            <Rectangle Style="{StaticResource fixed}" />
            <Rectangle Style="{StaticResource rotated}" />
        </StackPanel>
        <TextBlock Grid.Row="0" Grid.Column="0" 
                   Style="{StaticResource title}" Text="No rotation" />

        <StackPanel Grid.Row="0" Grid.Column="1" Background="Beige" Margin="5">
            <Rectangle Style="{StaticResource fixed}" />
            <Rectangle Style="{StaticResource rotated}">
                <Rectangle.RenderTransformOrigin>.5,.5</Rectangle.RenderTransformOrigin>
                <Rectangle.RenderTransform>
                    <RotateTransform Angle="20" />
                </Rectangle.RenderTransform>
            </Rectangle>
        </StackPanel>
        <TextBlock Grid.Row="0" Grid.Column="1"
                   Style="{StaticResource title}"
                   Text="RenderTransformOrigin" />

        <StackPanel Grid.Row="1" Grid.Column="0" Background="Beige" Margin="5">
            <Rectangle Style="{StaticResource fixed}" />
            <Rectangle Style="{StaticResource rotated}">
                <Rectangle.RenderTransform>
                    <RotateTransform Angle="20" CenterX="100" CenterY="40" />
                </Rectangle.RenderTransform>
            </Rectangle>
        </StackPanel>
        <TextBlock Grid.Row="1" Grid.Column="0"
                   Style="{StaticResource title}"
                   Text="RotateTransform Center" />

        <!-- The center coordinates relative to the Rectangle are the sum
             of both center coordinates, i.e. .5 + .5 = 1 (bottom-right corner) -->
        <StackPanel Grid.Row="1" Grid.Column="1" Background="Beige" Margin="5">
            <Rectangle Style="{StaticResource fixed}" />
            <Rectangle Style="{StaticResource rotated}">
                <Rectangle.RenderTransformOrigin>.5,.5</Rectangle.RenderTransformOrigin>
                <Rectangle.RenderTransform>
                    <RotateTransform Angle="20" CenterX="100" CenterY="40" />
                </Rectangle.RenderTransform>
            </Rectangle>
        </StackPanel>
        <TextBlock Grid.Row="1" Grid.Column="1" 
                   Style="{StaticResource title}"
                   Text="Both" />

    </Grid>
</Window>