gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 17:34:21 25 4
gpt4 key购买 nike

<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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com