简体   繁体   English

在代码中选择多个资源词典之一-WPF

[英]Choosing one of multiple resource dictionaries in code - WPF

I have developed several WPF resource dictionaries with distinct styles for controls grouped together. 我为组合在一起的控件开发了几种具有不同样式的WPF资源字典。

In my new project I cant decide which I like best so I would like a setting to enable the user to switch between them. 在我的新项目中,我无法决定最喜欢哪一个,因此我希望进行设置以使用户能够在它们之间进行切换。

At present I define the resource dictionary in the App.xaml file as follows: 目前,我在App.xaml文件中定义了资源字典,如下所示:

<Application.Resources> 
      <ResourceDictionary Source="/Styles/BlueStyle.xaml" />     
</Application.Resources>

Is it possible to put define this in C# code so that I can pick from a list of styles (perhaps from a dropdown box) Instead of being locked into one. 是否可以在C#代码中定义它,以便我可以从样式列表中选择(也许从下拉列表中选择),而不必锁定为一个。

Thanks in advance 提前致谢

Will set the resource at runtime: 将在运行时设置资源:

Application.Current.Resources.Source = new Uri("/Styles/BlueStyle.xaml", UriKind.RelativeOrAbsolute);

Or in a ComboBox_SelectionChanged (thats contains items like BlueStyle and RedStyle ): 或在ComboBox_SelectionChanged中(其中包含BlueStyleRedStyle类的项目):

ResourceDictionary dictionary = new ResourceDictionary();
dictionary.Source = new Uri(@"/Styles/" + comboBox.SelectedValue.ToString() + ".xaml", UriKind.Relative);
Application.Current.Resources.MergedDictionaries.Clear(); 
Application.Current.Resources.MergedDictionaries.Add(dictionary);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM