我定義了一個自定義畫筆,如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}"/>