c# - 如何从 XAML 中定义的 ResourceDictionary 检索 Brush 并将其应用于代码中的元素?

标签 c# wpf resourcedictionary

<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");

关于c# - 如何从 XAML 中定义的 ResourceDictionary 检索 Brush 并将其应用于代码中的元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12152803/

相关文章:

c# - RoutedEvent 隧道未到达子节点

wpf绑定(bind)到元素

c# - 如何从线程显示 Infragistics 的 UltraDesktopAlert

c# - HttpContext.Current.Cache 与 HttpRuntime.Cache

.net - VB.Net - 将处理程序添加到未知类型的对象?

wpf - 如何使用资源字典中 <Styles.Resources> 中定义的资源

ios - 如何访问我项目的资源文件夹中所有文件的列表?

c# - 以编程方式关闭 MessageDialog

c# - 序列化和反序列化 .NET 对象的最快方法

c# - 设置资源字典的源时无法识别 URI 前缀