【问题标题】:Apply custom brush in XAML在 XAML 中应用自定义画笔
【发布时间】:2015-05-29 07:51:07
【问题描述】:

我定义了一个自定义画笔,如 How to define and use resources in xaml so they can be used in C# 中所述。

但是,当我尝试将其分配给 Shape.FillShape.Stroke 属性时,如下所示:

<Rectangle Stroke="MyBrush"/>

我的应用程序因XamlParseException 而崩溃。那么如何只使用 XAML 代码来分配自定义画笔呢?

【问题讨论】:

    标签: c# wpf brush


    【解决方案1】:

    在 XAML 中,您不能简单地指定资源名称

    <Rectangle Stroke="{Binding Source={StaticResource OfferByBrand}}"/>
    

    如果你是通过代码绑定的,并且它本身已经预定义了,或者

    <Rectangle Stroke="{DynamicResource OfferByBrand}"/>
    

    如果在 res 中指定。 否则,您将需要通过 C# 使用

    Application.Current.Resources["BlaBrush"] as LinearGradientBrush
    

    请查看Resources

    【讨论】:

      【解决方案2】:

      ResourceDictionary 中创建Brush 资源,然后您可以在控件定义中引用它:

      <SolidColorBrush x:Key="MyBrush">#727272</SolidColorBrush>
      

      或者:

      <SolidColorBrush x:Key="MyBrush" Color="Red"/>
      

      用法:

      <Rectangle Stroke="{DynamicResource MyBrush}"/>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-01-23
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        • 2011-04-30
        • 1970-01-01
        相关资源
        最近更新 更多