折腾了半天,终于解决绝了运行时Telerik Controls在运行时修改主题的问题。
1、首先建立StyleViewModel类,继承INotifyPropertyChanged以实现双向绑定;
代码
1 public sealed class StyleViewModel : INotifyPropertyChanged
2 {
3 private Theme selectedTheme;
4 private readonly IEnumerable<Theme> themesSource = ThemeManager.StandardThemes.Select(a => a.Value);
5
6 public Theme SelectedTheme
7 {
8 get {
9 return this.selectedTheme ?? this.themesSource.First();
10 }
11 set {
12 this.selectedTheme = value;
13 this.PropertyChanged(this, new PropertyChangedEventArgs("SelectedTheme"));
14 }
15 }
16
17 public IEnumerable<Theme> ThemesSource
18 {
19 get { return this.themesSource; }
20 }
21
22 public event PropertyChangedEventHandler PropertyChanged;
23 }
2 {
3 private Theme selectedTheme;
4 private readonly IEnumerable<Theme> themesSource = ThemeManager.StandardThemes.Select(a => a.Value);
5
6 public Theme SelectedTheme
7 {
8 get {
9 return this.selectedTheme ?? this.themesSource.First();
10 }
11 set {
12 this.selectedTheme = value;
13 this.PropertyChanged(this, new PropertyChangedEventArgs("SelectedTheme"));
14 }
15 }
16
17 public IEnumerable<Theme> ThemesSource
18 {
19 get { return this.themesSource; }
20 }
21
22 public event PropertyChangedEventHandler PropertyChanged;
23 }
2、修改App.xaml.cs
新建方法ResetRootVisual,用来重新绘制根节点
代码
1 private void ResetRootVisual()
2 {
3 var rootVisual = Application.Current.RootVisual as Grid;
4 rootVisual.Children.Clear();
5 rootVisual.Children.Add(new MainPage());
6 }
2 {
3 var rootVisual = Application.Current.RootVisual as Grid;
4 rootVisual.Children.Clear();
5 rootVisual.Children.Add(new MainPage());
6 }
修改Application_Startup,创建StyleViewModel对象,并响应PropertyChanged事件
代码
1 private void Application_Startup(object sender, StartupEventArgs e)
2 {
3 this.customeStyle = new StyleViewModel();
4 this.customeStyle.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(customeStyle_PropertyChanged);
5
6 this.RootVisual = new Grid();
7 this.ResetRootVisual();
8 }
9
10 void customeStyle_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
11 {
12 StyleManager.ApplicationTheme = this.customeStyle.SelectedTheme;
13 this.ResetRootVisual();
14 }
2 {
3 this.customeStyle = new StyleViewModel();
4 this.customeStyle.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(customeStyle_PropertyChanged);
5
6 this.RootVisual = new Grid();
7 this.ResetRootVisual();
8 }
9
10 void customeStyle_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
11 {
12 StyleManager.ApplicationTheme = this.customeStyle.SelectedTheme;
13 this.ResetRootVisual();
14 }
新增GetCustomeStyle方法用于获取customeStyle
1 public StyleViewModel GetCustomeStyle()
2 {
3 return this.customeStyle;
4 }
2 {
3 return this.customeStyle;
4 }
3、创建界面控件
创建两个CmoboBox控件并对他们的SelectedItem进行双向绑定,方便查看双向绑定是否成功
代码
1 <telerik:RadComboBox HorizontalAlignment="Left" Margin="37,30,0,0" Name="radComboBox1" VerticalAlignment="Top" Width="250"
2 ItemsSource="{Binding ThemesSource, Mode=OneTime}" SelectedItem="{Binding SelectedTheme, Mode=TwoWay}">
3 </telerik:RadComboBox>
4 <telerik:RadComboBox HorizontalAlignment="Left" Margin="316,30,0,0" Name="radComboBox2" VerticalAlignment="Top" Width="250"
5 ItemsSource="{Binding ThemesSource, Mode=OneTime}" SelectedItem="{Binding SelectedTheme, Mode=TwoWay}" >
6 </telerik:RadComboBox>
7 <sdk:Label Height="28" HorizontalAlignment="Left" Margin="59,69,0,0" Name="label2" VerticalAlignment="Top" Width="120" Content="{Binding SelectedTheme, Mode=OneWay}" />
2 ItemsSource="{Binding ThemesSource, Mode=OneTime}" SelectedItem="{Binding SelectedTheme, Mode=TwoWay}">
3 </telerik:RadComboBox>
4 <telerik:RadComboBox HorizontalAlignment="Left" Margin="316,30,0,0" Name="radComboBox2" VerticalAlignment="Top" Width="250"
5 ItemsSource="{Binding ThemesSource, Mode=OneTime}" SelectedItem="{Binding SelectedTheme, Mode=TwoWay}" >
6 </telerik:RadComboBox>
7 <sdk:Label Height="28" HorizontalAlignment="Left" Margin="59,69,0,0" Name="label2" VerticalAlignment="Top" Width="120" Content="{Binding SelectedTheme, Mode=OneWay}" />
4、修改MainPage.xaml.cs,绑定DataContext
1 public MainPage()
2 {
3 InitializeComponent();
4
5 DataContext = (Application.Current as App).GetCustomeStyle();
6 }
2 {
3 InitializeComponent();
4
5 DataContext = (Application.Current as App).GetCustomeStyle();
6 }
5、最终效果图
总结:这个方法的缺点在于每次更改样式主题后都会重置用户界面为初始界面,不能保存用户的当前状态,如有方法保存用户当前状态,烦请告知,谢谢