2012-08-28 25 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/> 

Bây giờ tôi muốn lấy LinearGradientBrush từ ResourceDictonary và áp dụng nó tự động vào một nút làm màu nền trong wpf.Làm thế nào để lấy Brushes từ Resource Dictionary và áp dụng nó cho một phần tử dynamicallly trong wpf?

BtnGetBrushes.Background = Brushes.Green; 

Tôi muốn áp dụng màu trên thay vì điều này (Brushes.Green). tôi nên làm gì?

Trả lời

14

Giả sử ResourceDictionary của bạn có sẵn trong bối cảnh:

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

hoặc Mã

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

trình cho tôi trên Windows Phone 8.0 Silverlight – aloisdg

Các vấn đề liên quan