簡體   English   中英

如何從 XAML 中定義的 ResourceDictionary 檢索 Brush 並將其應用於代碼中的元素?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM