在 xaml 中设置时,WPF 附加属性不会触发后面的代码
我有一个自定义的 ClockFace UserControl,它具有允许更改颜色、字体和指针(作为 Path 对象)的属性。这用于自定义 TimePicker 和 Clock UserControls。在这些父控件中,可以在 xaml 中的 ClockFace 对象上设置 ClockFace 属性。 我想做的是公开这些 ClockFace 属性,以便可以在这两个父控件(例如 Clock 和 TimePicker 对象)上设置它们。我认为将它们设置为附加属性就可以解决问题,因此我尝试使用其中一个颜色属性。
public static readonly DependencyProperty HourTicksBrushProperty = DependencyProperty.RegisterAttached("HourTicksBrush", typeof(Brush), typeof(ClockFace), new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender));
public static void SetHourTicksBrush(DependencyObject element, Brush value)
{
element.SetValue(HourTicksBrushProperty, value);
}
public static Brush GetHourTicksBrush(DependencyObject element)
{
return (Brush)element.GetValue(HourTicksBrushProperty);
}
我可以在 Clock 所在的 xaml 中使用此附加属性:(Controls 是 xml 命名空间)
<Controls:Clock Controls:ClockFace.HourTicksBrush="Aqua" />
它编译得很好,但尽管附加的 HourTicksBrushProperty 中的默认值 (Brushes.Black) 显示,但父 Clock 上设置的值控制(Aqua)永远不会触发上述方法或改变颜色。我错过了什么吗?
需要明确的是,我希望能够在父控件上使用上述 xaml 来设置子 ClockFace 控件的 HourTicksBrush 属性。
任何帮助将不胜感激。
I have a custom ClockFace UserControl which has properties to allow colors, font and hands (as a Path objects) to be changed. This is used in custom TimePicker and Clock UserControls. In these parent controls, the ClockFace properties can be set on the ClockFace object in xaml just fine.
What I'm trying to do is expose these ClockFace properties so that they can be set on these two parent controls (eg. the Clock and TimePicker objects). I thought that making them Attached properties would do the trick, so I tried with one of the colour properties.
public static readonly DependencyProperty HourTicksBrushProperty = DependencyProperty.RegisterAttached("HourTicksBrush", typeof(Brush), typeof(ClockFace), new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsRender));
public static void SetHourTicksBrush(DependencyObject element, Brush value)
{
element.SetValue(HourTicksBrushProperty, value);
}
public static Brush GetHourTicksBrush(DependencyObject element)
{
return (Brush)element.GetValue(HourTicksBrushProperty);
}
I can use this attached property in the xaml where the Clock is with: (Controls is the xml namespace)
<Controls:Clock Controls:ClockFace.HourTicksBrush="Aqua" />
It compiles just fine, but although the default value (Brushes.Black) from the attached HourTicksBrushProperty shows, the value set on the parent Clock control (Aqua) never fire the above methods or change the colour. Am I missing something?
To be clear, I would like to be able to use the above xaml on a parent control to set the HourTicksBrush property of a child ClockFace control.
Any help would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
附加属性类中的那些“get”和“set”方法实际上只是您自己的代码隐藏的便捷方法。 XAML 解析器会忽略这些,并调用 element.SetValue 本身。如果您想响应 setter,则必须向
FrameworkPropertyMetadata
提供属性更改处理程序Those "get" and "set" methods in your attached property class are actually just convenience methods for your own codebehind. The XAML parser ignores these, calling element.SetValue itself. If you want to respond to a setter, you have to provide a property changed handler to the
FrameworkPropertyMetadata
使用类似的内容:
在 XAML 中
我不确定是否在relativesource中使用TemplatedParent或Self
Use something like:
and something like this in the XAML
I'm not sure whether to use TemplatedParent or Self in the RelativeSource