9
<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)の代わりに上記の色を適用したい。そのために私は何をすべきですか?

4

2 に答える 2

18

コンテキストで ResourceDictionary が利用可能であると仮定します。

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

またはコードで

button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush");
于 2012-08-28T04:45:29.683 に答える
4
BtnGetBrushes.Background = this.Resources["ButtonNormalBackgroundBrush"] as LinearGradientBrush;
于 2012-08-28T04:47:54.280 に答える