[英]Using XAML style on CodeBehind
伙計們我試圖將Style.xaml中的樣式用於我的樣式后面的代碼中,我有這樣的代碼
文件Style.xaml
<SolidColorBrush x:Key="FontGrey" Color="#FFC5C0C0"></SolidColorBrush>
在我的Apptest.xaml.cs文件上,我有這樣的代碼
txt.Foreground = new SolidColorBrush(Color.FromArgb(255, 252, 147, 25));
如果我想基於style.xaml更改前景色,該怎么辦? 我正在嘗試使用資源,但沒有用
注意:Style.xaml和Apptest.xaml是分開的
您可以使用以下語法在Silverlight中訪問定義的資源:
txt.Foreground = (SolidColorBrush)Application.Current.Resources["FontGrey"];
您可以將樣式放入Apptest.xaml中的Window.Resources中,如下所示:
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary
Source="Style1.xaml">
</ResourceDictionary>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
然后,在文件Apptest.xaml.cs后面的窗口代碼中,您可以訪問資源:
InitializeComponent();
txt.Foreground = Resources["FontGrey"] as SolidColorBrush;
如果假定資源可用,那么此代碼將為您工作:
txt.Foreground = (Brush)FindResource("FontGrey");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.