引用类型 DependencyProperty 的 WPF 样式

WPF styles for reference type DependencyProperty

我遇到了与 WPF 样式相关的问题。 让我们使用 class(或准备这样的)来包含 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 文件中定义样式并将 class MyTextBlock 的 2 个对象放在我的主 window 网格上:

<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 更改为 false 时:

textBlock1.myProperty.exampleValue = false;

exampleValue 也针对 textBlock2 进行了更改。

如我所见,textBlock1.myProperty 和 textBlock2.myProperty return 都具有相同的 hashCode。 这可能是因为我们首先创建了一个 myProperty 对象,然后 Setter 将它分配(复制)给每个 MyTextBlock 对象。 有没有办法在这里使用克隆?所以每个对象都会有自己的 "myProperty"?

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

<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>

它是 "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 的评论;)