【问题标题】:How to use a defined brush resource in XAML, from C#如何在 XAML 中使用已定义的画笔资源,来自 C#
【发布时间】:2009-08-08 06:49:21
【问题描述】:

到目前为止我有这个

<UserControl.Resource>
 <LinearGradientBrush x:Key="KeyDownBrush" .....>

现在我想在按下一个键时访问这个定义的资源,并用 C# 中的 KeyDownBrush 替换当前填充的对象。

我已经尝试过 this.Resource.Contains("KeyDownPress") 并且能够返回 True,所以我想我快到了,但我不确定如何访问该对象并将其正确解析为 Brush实例。

任何指导将不胜感激。

【问题讨论】:

    标签: c# wpf xaml resources


    【解决方案1】:

    在您的用户控件中:

    var brush = this.Resources["KeyDownBrush"] as LinearGradientBrush;
    

    应该做的伎俩。

    【讨论】:

    • 谢谢马特。我想我必须很接近,我似乎无法在我正在寻找的文档中找到一个例子。对于其他任何人,我使用的 C# 看起来像 System.Windows.Media.LinearGraidentBrush aBrush = (System.Windows.Media.LinearGradientBrush)this.Resources["KeyDownBrush"]; aRectangle.Fill = aBrush;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 2011-03-19
    • 2016-12-18
    相关资源
    最近更新 更多