如何在运行时修改Telerik控件的主题

折腾了半天,终于解决绝了运行时Telerik Controls在运行时修改主题的问题。

修改方法参照了:http://blogs.telerik.com/blogs/posts/10-03-17/changing_silverlight_application_themes_at_runtime.aspx

1、首先建立StyleViewModel类,继承INotifyPropertyChanged以实现双向绑定;

ExpandedBlockStart.gif 代码
 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、修改App.xaml.cs

新建方法ResetRootVisual,用来重新绘制根节点

ExpandedBlockStart.gif 代码
1           private   void  ResetRootVisual()
2          {
3              var rootVisual  =  Application.Current.RootVisual  as  Grid;
4              rootVisual.Children.Clear();
5              rootVisual.Children.Add( new  MainPage());
6          }

 

修改Application_Startup,创建StyleViewModel对象,并响应PropertyChanged事件

ExpandedBlockStart.gif 代码
 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          }

 

新增GetCustomeStyle方法用于获取customeStyle

1  public  StyleViewModel GetCustomeStyle()
2  {
3       return   this .customeStyle;
4  }

 

 

 3、创建界面控件

创建两个CmoboBox控件并对他们的SelectedItem进行双向绑定,方便查看双向绑定是否成功

 

ExpandedBlockStart.gif 代码
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} "    />

 

 4、修改MainPage.xaml.cs,绑定DataContext

1  public  MainPage()
2  {
3      InitializeComponent();
4              
5      DataContext  =  (Application.Current  as  App).GetCustomeStyle();
6  }

 

5、最终效果图

 

 

 

总结:这个方法的缺点在于每次更改样式主题后都会重置用户界面为初始界面,不能保存用户的当前状态,如有方法保存用户当前状态,烦请告知,谢谢

posted on 2010-08-03 16:57  阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/leisky/archive/2010/08/03/1791385.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值