如何从 XAML 中定义的 ResourceDictionary 检索 Brush 并将其应用于代码中的元素? [英] How can you retrieve a Brush from a ResourceDictionary defined in XAML and apply it to an element in code?

查看:46
本文介绍了如何从 XAML 中定义的 ResourceDictionary 检索 Brush 并将其应用于代码中的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<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 中的背景颜色.

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

 BtnGetBrushes.Background = Brushes.Green;

我想应用上面的颜色而不是这个(Brushes.Green).我该怎么做?

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

推荐答案

假设您的 ResourceDictionary 在上下文中可用:

Assuming your ResourceDictionary available in the context:

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

或在代码中

button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");

这篇关于如何从 XAML 中定义的 ResourceDictionary 检索 Brush 并将其应用于代码中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆