有没有办法将动态资源分配给代码中的依赖项属性

发布于 2025-02-04 03:55:37 字数 972 浏览 3 评论 0原文

我在资源词典中定义了一个固体纸笔,如下所示:

 <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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2025-02-11 03:55:37

不可能将动态资源分配给依赖项属性的默认值。 (这也不是有意义的,因为默认值一次是评估一次,而动态资源可以按定义更改),

但是,如果您的目标是将实际的依赖性属性值分配给动态资源,从而有效地将属性绑定到动态资源中,则该网站上已经存在描述如何做到这一点的答案。 这似乎是最好的现有解决方案。

我将其发布为答案,而不是将其标记为重复,因为您的问题专门询问有关设置默认值值。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文