我需要允许用户在使用telerik WPF控件创建的应用程序中动态更改主题。
我正在设置对XAML中每个Telerik控件的绑定,如下所示:
XAML:
telerik:StyleManager.Theme="{Binding SelectedSMTheme, Mode=TwoWay}"
ViewModel:
private Theme selectedSMTheme;
public Theme SelectedSMTheme
{
get
{
return selectedSMTheme;
}
set
{
selectedSMTheme = value;
RaisePropertyChange("SelectedSMTheme");
}
}
并在用户选择主题时更改此SelectedSMTheme
。
更改主题:
SelectedSMTheme = new Expression_DarkTheme();
在运行应用程序时,是否还有其他方法可以更改telerik控件的主题。因为,在这里,我需要为整个应用程序中的每个控件的每个n指定telerik:StyleManager.Theme
。
答案 0 :(得分:1)
您可以使用StyleManager.ApplicationTheme
设置初始主题。设置此属性会影响应用程序中的所有控件。
您的App.xaml.cs构造函数应如下所示:
public partial class App : Application
{
public App()
{
StyleManager.ApplicationTheme = new Expression_DarkTheme();
this.InitializeComponent();
}
}
要在运行时切换主题,您应该清除应用程序资源并添加新资源。
private void btnChangeTheme_Click(object sender, RoutedEventArgs e)
{
Application.Current.Resources.MergedDictionaries.Clear();
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
{
Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)
});
}
您必须记住要从安装文件夹中的Binaries.NoXaml文件夹中添加所需的程序集(在我的情况下为C:\Program Files (x86)\Progress\Telerik UI for WPF R2 2018\Binaries.NoXaml
):
Telerik.Windows.Controls.dll
Telerik.Windows.Controls.Input.dll
Telerik.Windows.Themes.Expression_Dark.dll
和Telerik.Windows.Themes.Green.dll
请阅读以下文章以获取更多信息:
https://docs.telerik.com/devtools/wpf/styling-and-appearance/how-to/styling-apperance-themes-runtime
答案 1 :(得分:1)
我正在使用与kmatyaszek提供的解决方案类似的解决方案,除了,我是在ComboBox中这样做的:
private void StyleCombo_OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selectedTheme = StyleCombo.SelectedItem as ThemeNames;
Application.Current.Resources.MergedDictionaries.Clear();
// XAML
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.DataVisualization.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.Docking.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.Pivot.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.PivotFieldList.xaml", UriKind.RelativeOrAbsolute)
});
Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary
{
Source = new Uri("/Telerik.Windows.Themes." + selectedTheme.Value + ";component/Themes/Telerik.Windows.Controls.VirtualGrid.xaml", UriKind.RelativeOrAbsolute)
});
}
您已经在评论部分中说过,某些控件没有动态更改-这是NoXaml库的已知问题。我可以向您推荐的是手动为这些控件设置参数。就我而言,它看起来像这样:
// Main Grid
if (selectedTheme.Name == "Visual Studio 2013 Dark")
{
VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Dark);
App.StronaGlowna.MainGrid.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#FF1E1E1E");
}
if (selectedTheme.Name == "Visual Studio 2013 Blue")
{
VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Blue);
}
if (selectedTheme.Name == "Visual Studio 2013")
{
VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Light);
App.StronaGlowna.MainGrid.Background = Brushes.White;
}
if (selectedTheme.Name == "Dark")
{
VisualStudio2013Palette.LoadPreset(VisualStudio2013Palette.ColorVariation.Dark);
App.StronaGlowna.MainGrid.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#FF3D3D3D");
}
if (selectedTheme.Name == "Green")
{
GreenPalette.LoadPreset(GreenPalette.ColorVariation.Dark);
App.StronaGlowna.MainGrid.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#FF1D1E21");
}
if (selectedTheme.Name == "Green Light")
{
GreenPalette.LoadPreset(GreenPalette.ColorVariation.Light);
App.StronaGlowna.MainGrid.Background = (SolidColorBrush)new BrushConverter().ConvertFrom("#FFE0E0E0");
}
if (selectedTheme.Name == "Vista" ||
selectedTheme.Name == "Visual Studio 2013 Blue" || selectedTheme.Name == "Office Black" ||
selectedTheme.Name == "Office Blue" ||
selectedTheme.Name == "Office Silver" || selectedTheme.Name == "Summer" ||
selectedTheme.Name == "Transparent" || selectedTheme.Name == "Windows 7")
{
App.StronaGlowna.MainGrid.Background = Brushes.White;
}
Telerik支持人员在1张支持凭单中向我提供了此解决方案。您可以在文档中找到大多数控件的十六进制颜色,例如this one is for Office2013 theme。另外,请记住,某些WPF控件不受样式影响(我认为示例之一是TextBox),因此,如果您使用的是普通WPF控件,则它们可能保持不变,并要求您对新颜色进行硬编码。