我定义了一个自定义画笔,如How to define and use resources in xaml so they can be used in C#中所述。在XAML中应用自定义画笔
但是当我尝试这个分配给Shape.Fill
或Shape.Stroke
房产这样的:
<Rectangle Stroke="MyBrush"/>
与XamlParseException
我的应用程序崩溃。那么我怎样才能通过使用XAML代码分配自定义画笔?
我定义了一个自定义画笔,如How to define and use resources in xaml so they can be used in C#中所述。在XAML中应用自定义画笔
但是当我尝试这个分配给Shape.Fill
或Shape.Stroke
房产这样的:
<Rectangle Stroke="MyBrush"/>
与XamlParseException
我的应用程序崩溃。那么我怎样才能通过使用XAML代码分配自定义画笔?
那么一次在XAML内部,您不能指定资源名称
<Rectangle Stroke="{Binding Source={StaticResource OfferByBrand}}"/>
如果通过代码绑定和其预定的存在本身或
<Rectangle Stroke="{DynamicResource OfferByBrand}"/>
如果水库内指定。 否则作为回答您需要使用
Application.Current.Resources["BlaBrush"] as LinearGradientBrush
做检查出Resources
通过C#访问在ResourceDictionary
创建Brush
资源,然后你可以参考一下,在你的控制定义:
<SolidColorBrush x:Key="MyBrush">#727272</SolidColorBrush>
或者:
<SolidColorBrush x:Key="MyBrush" Color="Red"/>
用法:
<Rectangle Stroke="{DynamicResource MyBrush}"/>