How get Brushes from Resource Dictionary and apply

2019-04-22 16:43发布

<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<LinearGradientBrush x:Key="ButtonNormalBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0">
    <GradientStop Color="#C10099FF" Offset="0"/>
    <GradientStop Color="#C16699CC" Offset="1"/>
    <GradientStop Color="#C1006699" Offset="0.49"/>
</LinearGradientBrush>
<ResourceDictionary/>

Now i want to get LinearGradientBrush from ResourceDictonary and apply it dynamically to a button as background color in wpf.

 BtnGetBrushes.Background = Brushes.Green;

I want to apply the above color instead of this(Brushes.Green). what should i do for that ?

2条回答
不美不萌又怎样
2楼-- · 2019-04-22 17:11

Assuming your ResourceDictionary available in the context:

<Button Background="{DynamicResource ResourceKey=ButtonNormalBackgroundBrush}" />

or in Code

button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");
查看更多
太酷不给撩
3楼-- · 2019-04-22 17:32
BtnGetBrushes.Background = this.Resources["ButtonNormalBackgroundBrush"] as LinearGradientBrush;
查看更多
登录 后发表回答