2012-08-28 11 views
7
<ResourceDictionary 
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:Microsoft_Windows_Themes="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 

<LinearGradientBrush x:Key="ButtonNormalBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0"> 
    <GradientStop Color="#C10099FF" Offset="0"/> 
    <GradientStop Color="#C16699CC" Offset="1"/> 
    <GradientStop Color="#C1006699" Offset="0.49"/> 
</LinearGradientBrush> 
<ResourceDictionary/> 

Ora voglio ottenere LinearGradientBrush da ResourceDictonary e applicarlo dinamicamente a un pulsante come colore di sfondo in wpf.Come ottenere i pennelli dal dizionario delle risorse e applicarli a un elemento dinamicamente in wpf?

BtnGetBrushes.Background = Brushes.Green; 

Voglio applicare il colore sopra invece di questo (Brushes.Green). cosa dovrei fare per quello?

risposta

14

Assumendo che il ResourceDictionary disponibili nel contesto:

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

o in codice

button.Background = (Brush)FindResource("ButtonNormalBackgroundBrush"); 
4
BtnGetBrushes.Background = this.Resources["ButtonNormalBackgroundBrush"] as LinearGradientBrush; 
+0

per me funziona su Windows Phone 8.0 di Silverlight – aloisdg

Problemi correlati