2014-01-14 37 views
1

我已经开发了几个不同风格的WPF资源字典,用于控件分组在一起。选择代码中的多个资源字典之一 - WPF

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

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

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

是否有可能从把在C#代码中定义,这样我可以从样式列表中选择(也许一个下拉框)而不是被锁定在一个。

在此先感谢

回答

1

将设置资源在运行时:

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

或者在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);