2
添加MergedDictionaries將樣式我有這個style
在資源文件:通過代碼
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
<Style TargetType="TextBox" x:Key="StandardTextBox"/>
<Setter Property="Foreground" Value="{StaticResource Color1}"/>
</Style>
</ResourceDictionary>
(Colors.xaml包含我brushes
)
使用style
我的代碼:
ResourceDictionary TetxboxStyles = new ResourceDictionary();
TetxboxStyles.Source = (new Uri("TextboxStyles.xaml", UriKind.RelativeOrAbsolute));
Resources.MergedDictionaries.Add(TetxboxStyles);
tb_input.Style = (Style)Find("StandardTextBox");
這種方法沒有問題,但當我通過代碼而不是TextboxStyles文件動態添加顏色資源時,它不起作用:
ResourceDictionary TetxboxStyles = new ResourceDictionary();
TetxboxStyles.Source = (new Uri("TextboxStyles.xaml", UriKind.RelativeOrAbsolute));
//Adding the Colors.xaml Resource
ResourceDictionary Colors = new ResourceDictionary();
brushes.Source = (new Uri("Colors.xaml", UriKind.RelativeOrAbsolute));
TetxboxStyles.MergedDictionaries.Add(Colors);
Resources.MergedDictionaries.Add(TetxboxStyles);
tb_input.Style = (Style)Find("StandardTextBox");
輸出錯誤:
System.Windows.Markup.XamlParseException
"{DependencyProperty.UnsetValue}"
你想通過資源字典 「Brushes.xaml」 畫你的文本框?包含刷TextboxStyles.xaml , 文件: – StepUp
文件,其中包含風格Colors.xaml 文本框具有TextboxtStyles.xaml它的風格,同時你爲什麼需要TextboxSyles.xaml具有Colors.xaml – MyNameIsHans
它的顏色/畫筆將多個ResourceDictionaries應用於一個控件(到您的TextBox)?因爲您應該在一個ResourceDictionary中編寫自己的模板,您可以在其中定義顏色和控件的畫筆,如https://msdn.microsoft.com/de-de/library/ms752068(v=vs.85).aspx – StepUp