轉帖|其它|編輯:郝浩|2011-04-20 10:39:14.000|閱讀 2886 次
概述:折騰了半天,終于解決絕了運行時Telerik Controls在運行時修改主題的問題。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
折騰了半天,終于解決絕了運行時Telerik Controls在運行時修改主題的問題。
1、首先建立StyleViewModel類,繼承INotifyPropertyChanged以實現雙向綁定;
public sealed class StyleViewModel : INotifyPropertyChanged
{
private Theme selectedTheme;
private readonly IEnumerable<Theme> 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<Theme> 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進行雙向綁定,方便查看雙向綁定是否成功
<telerik:RadComboBox HorizontalAlignment=
"Left" Margin="37,30,0,0" Name="radComboBox1"
VerticalAlignment="Top" Width="250"
ItemsSource="{Binding ThemesSource, Mode=OneTime}" SelectedItem="{Binding SelectedTheme, Mode=TwoWay}">
</telerik:RadComboBox>
<telerik:RadComboBox HorizontalAlignment=
"Left" Margin="316,30,0,0" Name=
"radComboBox2" VerticalAlignment="Top" Width="250"
ItemsSource="{Binding ThemesSource, Mode=OneTime}" SelectedItem="{Binding SelectedTheme, Mode=TwoWay}" >
</telerik:RadComboBox>
<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
public MainPage()
{
InitializeComponent();
DataContext = (Application.Current as App).GetCustomeStyle();
}
5、最終效果圖
總結:這個方法的缺點在于每次更改樣式主題后都會重置用戶界面為初始界面,不能保存用戶的當前狀態
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自:網絡轉載