有没有办法将动态资源分配给代码中的依赖项属性
我在资源词典中定义了一个固体纸笔,如下所示:
<Color x:Key="ColorGray100">#F5F6F7</Color>
<SolidColorBrush x:Key="BrushGray100" Color="{DynamicResource ColorGray100}" />
我在后面的代码中定义了用户控制的依赖项属性,如下所示:
/// <summary>
/// get/set the brush for the pattern ring.
/// The pattern ring is a full circle which the progress ring is drawn over.
/// This Brush value is defaulted to Brushes.LightGray
/// </summary>
public Brush PatternRingBrush
{
get => (Brush)GetValue(PatternRingBrushProperty);
set => SetValue(PatternRingBrushProperty, value);
}
public static readonly DependencyProperty PatternRingBrushProperty =
DependencyProperty.Register("PatternRingBrush", typeof(Brush), typeof(UXProgressRing),
new UIPropertyMetadata(Brushes.LightGray));
如何为默认的uipropertymetardadatata value分配“ brushgray100” solidcolorbrush,而不是刷子。
I have a SolidColorBrush defined in a Resource Dictionary as follows:
<Color x:Key="ColorGray100">#F5F6F7</Color>
<SolidColorBrush x:Key="BrushGray100" Color="{DynamicResource ColorGray100}" />
I have a Dependency Property defined in the code behind for a user control as follows:
/// <summary>
/// get/set the brush for the pattern ring.
/// The pattern ring is a full circle which the progress ring is drawn over.
/// This Brush value is defaulted to Brushes.LightGray
/// </summary>
public Brush PatternRingBrush
{
get => (Brush)GetValue(PatternRingBrushProperty);
set => SetValue(PatternRingBrushProperty, value);
}
public static readonly DependencyProperty PatternRingBrushProperty =
DependencyProperty.Register("PatternRingBrush", typeof(Brush), typeof(UXProgressRing),
new UIPropertyMetadata(Brushes.LightGray));
How can I assign the "BrushGray100" SolidColorBrush as the default UIPropertyMetadata value instead of Brushes.LightGray?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可能将动态资源分配给依赖项属性的默认值。 (这也不是有意义的,因为默认值一次是评估一次,而动态资源可以按定义更改),
但是,如果您的目标是将实际的依赖性属性值分配给动态资源,从而有效地将属性绑定到动态资源中,则该网站上已经存在描述如何做到这一点的答案。 这似乎是最好的现有解决方案。
我将其发布为答案,而不是将其标记为重复,因为您的问题专门询问有关设置默认值值。
It is not possible to assign a dynamic resource to dependency property's default value. (Nor would it make sense because the default value is evaluated once, whereas a dynamic resource can by definition change)
However, if your goal is to assign the actual dependency property value to a dynamic resource, effectively binding the property to the dynamic resource, there already exist answers on this site that describe how to do that. This appears to be the best existing solution.
I'm posting this as an answer instead of marking as duplicate because your question specifically asks about setting the default value.