如何在运行时修改Telerik控件的主题
1个回答
展开全部
1、首先建立StyleViewModel类,继承INotifyPropertyChanged以实现双向绑定; public sealed class StyleViewModel : INotifyPropertyChanged{private Theme selectedTheme;
private readonly IEnumerable themesSource = ThemeManager.StandardThemes.Select(a => a.Value);
public Theme SelectedTheme{get {return this.selectedTheme ? this.themesSource.First();}set {this.selectedTheme = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("SelectedTheme"));}}
public IEnumerable ThemesSource{get { return this.themesSource; }}
public event PropertyChangedEventHandler PropertyChanged;
}2、修改App.xaml.cs新建方法ResetRootVisual,用来重新绘制根节点 private void ResetRootVisual(){var rootVisual = Application.Current.RootVisual as Grid;
rootVisual.Children.Clear();
rootVisual.Children.Add(new MainPage());
}修改Application_Startup,创建StyleViewModel对象,并响应PropertyChanged事件 private void Application_Startup(object sender, StartupEventArgs e){this.customeStyle = new StyleViewModel();
this.customeStyle.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(customeStyle_PropertyChanged);
this.RootVisual = new Grid();
this.ResetRootVisual();}void customeStyle_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e){StyleManager.ApplicationTheme = this.customeStyle.SelectedTheme;
this.ResetRootVisual();
}新增GetCustomeStyle方法用于获取customeStyle public StyleViewModel GetCustomeStyle(){return this.customeStyle;
} 3、创建界面控件创建两个CmoboBox控件并对他们的SelectedItem进行双向绑定,方便查看双向绑定是否成功
4、修改MainPage.xaml.cs,绑定DataContext public MainPage(){InitializeComponent();}5、最终效果图 总结:这个方法的缺点在于每次更改样式主题后都会重置用户界面为初始界面,不能保存用户的当前状态
private readonly IEnumerable themesSource = ThemeManager.StandardThemes.Select(a => a.Value);
public Theme SelectedTheme{get {return this.selectedTheme ? this.themesSource.First();}set {this.selectedTheme = value;
this.PropertyChanged(this, new PropertyChangedEventArgs("SelectedTheme"));}}
public IEnumerable ThemesSource{get { return this.themesSource; }}
public event PropertyChangedEventHandler PropertyChanged;
}2、修改App.xaml.cs新建方法ResetRootVisual,用来重新绘制根节点 private void ResetRootVisual(){var rootVisual = Application.Current.RootVisual as Grid;
rootVisual.Children.Clear();
rootVisual.Children.Add(new MainPage());
}修改Application_Startup,创建StyleViewModel对象,并响应PropertyChanged事件 private void Application_Startup(object sender, StartupEventArgs e){this.customeStyle = new StyleViewModel();
this.customeStyle.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(customeStyle_PropertyChanged);
this.RootVisual = new Grid();
this.ResetRootVisual();}void customeStyle_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e){StyleManager.ApplicationTheme = this.customeStyle.SelectedTheme;
this.ResetRootVisual();
}新增GetCustomeStyle方法用于获取customeStyle public StyleViewModel GetCustomeStyle(){return this.customeStyle;
} 3、创建界面控件创建两个CmoboBox控件并对他们的SelectedItem进行双向绑定,方便查看双向绑定是否成功
4、修改MainPage.xaml.cs,绑定DataContext public MainPage(){InitializeComponent();}5、最终效果图 总结:这个方法的缺点在于每次更改样式主题后都会重置用户界面为初始界面,不能保存用户的当前状态
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询