Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在code-WPF中选择多个资源字典之一_C#_Wpf_Resourcedictionary - Fatal编程技术网

C# 在code-WPF中选择多个资源字典之一

C# 在code-WPF中选择多个资源字典之一,c#,wpf,resourcedictionary,C#,Wpf,Resourcedictionary,我已经为组合在一起的控件开发了几个具有不同样式的WPF资源字典 在我的新项目中,我无法决定我最喜欢哪一个,所以我想设置一个设置,让用户能够在它们之间切换 目前我在App.xaml文件中定义资源字典如下: <Application.Resources> <ResourceDictionary Source="/Styles/BlueStyle.xaml" /> </Application.Resources> 是否可以在C#代码中定

我已经为组合在一起的控件开发了几个具有不同样式的WPF资源字典

在我的新项目中,我无法决定我最喜欢哪一个,所以我想设置一个设置,让用户能够在它们之间切换

目前我在App.xaml文件中定义资源字典如下:

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

是否可以在C#代码中定义它,这样我就可以从样式列表(可能是从下拉框中)中进行选择,而不是锁定在一个样式列表中


提前感谢

将在运行时设置资源:

Application.Current.Resources.Source = new Uri("/Styles/BlueStyle.xaml", UriKind.RelativeOrAbsolute);
或者在组合框中选择更改(其中包含
BlueStyle
RedStyle
等项):

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);