- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有与 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/
我有一个包含只读 DependencyProperty 的类。从这个类中,我想绑定(bind)到只读 DependencyProperty到另一个只读 DepenendencyProperty另一个类
我有一个带有 ComboBox 的控件: 这是PropertyChangedCallback为属性(property) SelectedTrace在包含 ComboBox 的 OuterContro
我是 WPF/MVVM 的新手,我发现的示例似乎没有涵盖我面临的问题。 我有一个用于管理相当复杂的业务配置对象的屏幕。在 MVVM 中,我认为这意味着我应该具有以下内容: 具有接近于零逻辑的 XAML
我正在探索 WPF 世界,我在网上找到了一个关于如何在 xml 上使用绑定(bind)的好例子 http://www.codeproject.com/Articles/37854/How-to-Per
我有一个自定义类 MyPerson。所有(相关)属性都实现 INotifyPropertyChanged。 我创建了一个 UserControl 来显示它,一切正常。绑定(bind)到 MyPerso
我试图在我的代码中使用 tis 依赖属性,但它给了我错误,说默认值类型与属性“MyProperty”的类型不匹配。 但 short 应该接受 0 作为默认值。 如果我尝试给它一个 null 作为默认值
我在我的用户控件中定义了这样的属性: public bool IsSelected { get { return (bool)GetValue(IsSelectedProperty);
在我的用户控件中: public ODIF.DeviceChannel Channel { get { return (ODIF.DeviceChannel)GetValue(ChannelD
我在 WPF 应用程序的 ViewModel 中有这个简单的示例: class VM_DiskPartition : DependencyObject { // (...) Other
在previous post中我问如何将属性注册为 DependencyProperty。我得到了答案并且效果很好。 但现在我想在单击时向此 DependencyProperty 添加一些项目。这是行
我有两个对象绑定(bind)到同一个 dependencyProperty(在 Silverlight 中)。有没有办法确定这两个对象中的哪一个改变了属性?我想根据这些信息采取不同的行动。 不幸的是,
我正在将 XAML 序列化为文件并再次读取它(使用 XamlWriter 和 XamlReader)。 如果从未为元素设置 DependencyProperty(如 FrameworkElement
我有一个名为 ChartView 的用户控件。我有一个 ObservableCollection 类型的属性。我在 ChartView 中实现了 INotifyPropertyChanged。 Cha
虽然网络上的大多数代码示例都使用 DependencyProperties 的静态声明,但我发现在某些情况下它们被定义为公共(public)只读实例成员。 将 DependencyProperty 定
我有析构函数问题。这是重现问题的代码: class DPDemo : DependencyObject { public DPDemo() { } ~DPDemo()
在我的 XAML 中,我有一个带有 DependencyProperty 的对象: 我还有MouseUp处理程序 TextBlock .我如何获得 OverWidth属性值在里面? 最佳答案 这似乎
我正在创建一个模仿 AppBarButton 的自定义控件(但具有自定义功能,因此我们无法从 AppBarButton 派生)。 我的问题是 AppBarButton 的 Icon 属性。该属性本身采
如何将属性名称(字符串形式)转换为 DependencyProperty? 我有一组属性名称、它的字符串值和一个 DependencyObject。现在我想将这些属性值设置为 DependencyOb
我的控件有一个映射到私有(private)变量的属性。设置属性时,我还需要存储某个其他变量。当属性的私有(private)变量由我自己的控制代码设置时,一定不会出现这种特殊处理。一切顺利。 我现在需要
我正在尝试创建一个用户控件,其中包含一些 DependencyProperties,这些属性会转发给用户控件中的子控件。经过几次尝试,我开始工作了。为了测试一个小例子。 在示例中,我有一个名为 Ctr
我是一名优秀的程序员,十分优秀!