Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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/8/grails/5.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
C# 引用类型DependencyProperty的WPF样式_C#_Wpf_Dependency Properties_Wpf Style - Fatal编程技术网

C# 引用类型DependencyProperty的WPF样式

C# 引用类型DependencyProperty的WPF样式,c#,wpf,dependency-properties,wpf-style,C#,Wpf,Dependency Properties,Wpf Style,我在WPF风格方面有问题。 让我们使用类(或准备类)来包含DependencyProperty public class MyProperty : DependencyObject { public static readonly DependencyProperty exampleValueProperty = DependencyProperty.Register("exampleValue", typeof(bool), typeof(MyProperty)); publ

我在WPF风格方面有问题。 让我们使用类(或准备类)来包含DependencyProperty

public class MyProperty : DependencyObject
{
    public static readonly DependencyProperty exampleValueProperty = DependencyProperty.Register("exampleValue", typeof(bool), typeof(MyProperty));

    public bool exampleValue
    {
        get { return (bool)this.GetValue(exampleValueProperty); }
        set { this.SetValue(exampleValueProperty, value); }
    }
}

public class MyTextBlock : TextBlock
{
    public static readonly DependencyProperty myPropertyProperty= DependencyProperty.Register(
        "myProperty", typeof(MyProperty), typeof(MyTextBlock));
    public MyProperty myProperty
    {
        get
        {
            return (MyProperty)this.GetValue(myPropertyProperty);
        }
        set
        {
            this.SetValue(myPropertyProperty, value);
        }
    }
}
现在在xaml文件中定义样式,并将MyTextBlock类的两个对象放在我的主窗口网格上:

<Window x:Class="WpfApplication1.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:WpfApplication1"
    xmlns:custom="clr-namespace:WpfCustomControlLibrary1;assembly=WpfCustomControlLibrary1"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <Style TargetType="{x:Type custom:MyTextBlock}">
            <Setter Property="Background" Value="Aqua" />
            <Setter Property="myProperty">
                <Setter.Value>
                    <custom:MyProperty exampleValue="true" />
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</Window.Resources>
<Grid RenderTransformOrigin="0.514,0.47" Margin="100,0,0,0">
    <custom:MyTextBlock x:Name="textBlock1" Height="80" Width="80" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,104,62.8,0" />
    <custom:MyTextBlock x:Name="textBlock2" Height="80" Width="80" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,200,62.8,0" />
</Grid>
</Window>
exampleValue也为textBlock2更改

如我所见,textBlock1.myProperty和textBlock2.myProperty都返回相同的哈希代码。 这可能是因为我们首先创建一个myProperty对象,然后Setter将其分配(复制)给每个MyTextBlock对象。 有没有办法在这里使用克隆?那么每个对象都有自己的“myProperty”

我知道,如果我定义每个对象的属性,这个方法将正常工作(但它看起来像是解决方法,而不是解决方案):


它与“MyProperty”的实例相同,因为您将其创建为资源。 默认情况下,资源是共享/静态的。 可能将“x:Shared”设置为false会有帮助:

<ResourceDictionary>
    <Style TargetType="{x:Type custom:MyTextBlock}" x:Shared="false">
        <Setter Property="Background" Value="Aqua" />
        <Setter Property="myProperty">
            <Setter.Value>
                <custom:MyProperty exampleValue="true" />
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

无论如何,我不确定它是否会起作用,因为它是MyTextBlock控件的默认无键样式,并且可能会被缓存

如果有效,则每个MyTextBlock控件都有一个MyProperty实例,但“exampleValue”的值仍然相同


编辑:抱歉,没有看到@Brannon的评论;)

你的风格定义在哪里?你在上面设置了x:共享吗?太好了!它起作用了。我的样式中缺少x:Shared=“False”。非共享的缺点是:您正在创建大量重复的样式和对象,这些样式和对象可能是您想要的,也可能不是您想要的。您可以将
MyProperty
a
Freezable
,然后在您即将更改代码中的值时,将强制您创建一个新的属性实例。
<custom:MyTextBlock x:Name="textBlock1" Height="80"  HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,104,62.8,0">
    <custom:MyTextBlock.myProperty>
        <custom:MyProperty exampleValue="False"/>
    </custom:MyTextBlock.myProperty>
</custom:MyTextBlock>
<custom:MyTextBlock x:Name="textBlock2" Height="80"  HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,200,62.8,0">
    <custom:MyTextBlock.myProperty>
        <custom:MyProperty exampleValue="False"/>
    </custom:MyTextBlock.myProperty>
</custom:MyTextBlock>
<ResourceDictionary>
    <Style TargetType="{x:Type custom:MyTextBlock}" x:Shared="false">
        <Setter Property="Background" Value="Aqua" />
        <Setter Property="myProperty">
            <Setter.Value>
                <custom:MyProperty exampleValue="true" />
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>