[英]How can you retrieve a Brush from a ResourceDictionary defined in XAML and apply it to an element in code?
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero">
<LinearGradientBrush x:Key="ButtonNormalBackgroundBrush"
StartPoint = "0.5,0"
EndPoint = "0.5,1">
<GradientStop Color="#C10099FF" Offset="0"/>
<GradientStop Color="#C16699CC" Offset="1"/>
<GradientStop Color="#C1006699" Offset="0.49"/>
</LinearGradientBrush>
<ResourceDictionary/>
现在我想从 ResourceDictonary 获取 LinearGradientBrush 并将其动态应用到按钮作为 wpf 中的背景颜色。
BtnGetBrushes.Background = Brushes.Green;
我想应用上面的颜色而不是这个(Brushes.Green)。 我该怎么做?
假设您的 ResourceDictionary 在上下文中可用:
<Button Background="{DynamicResource ResourceKey=ButtonNormalBackgroundBrush}" />
或在代码中
button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");
BtnGetBrushes.Background = this.Resources["ButtonNormalBackgroundBrush"] as LinearGradientBrush;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.