gpt4 book ai didi

c# - 引用类型 DependencyProperty 的 WPF 样式

转载 作者:行者123 更新时间:2023-11-30 17:35:11 24 4
gpt4 key购买 nike

我有与 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 类的 2 个对象放在我的主窗口网格上:

<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 都返回相同的 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 的评论;)

关于c# - 引用类型 DependencyProperty 的 WPF 样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41486750/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com