动态更改主题 Telerik WPF

Upe*_*nda 5 c# wpf themes telerik

我需要允许用户在使用 Telerik WPF 控件创建的应用程序中动态更改主题。

我正在为 XAML 中的每个 Telerik 控件设置绑定,如下所示:

XAML:

telerik:StyleManager.Theme="{Binding SelectedSMTheme, Mode=TwoWay}"
Run Code Online (Sandbox Code Playgroud)

视图模型:

    private Theme selectedSMTheme;
    public Theme SelectedSMTheme
    {
        get
        {
            return selectedSMTheme;
        }
        set
        {
            selectedSMTheme = value;
            RaisePropertyChange("SelectedSMTheme");
        }
    }
Run Code Online (Sandbox Code Playgroud)

SelectedSMTheme在用户选择主题时更改此设置。

改变主题:

SelectedSMTheme = new Expression_DarkTheme();
Run Code Online (Sandbox Code Playgroud)

有没有其他方法可以在运行应用程序时更改 Telerik 控件的主题。因为,在这里我需要为telerik:StyleManager.Theme整个应用程序中的每个控件指定每个控件。

kma*_*zek 1

您可以用来StyleManager.ApplicationTheme设置初始主题。设置此属性会影响应用程序中的所有控件。

您的 App.xaml.cs 构造函数应如下所示:

public partial class App : Application
{
    public App()
    {
        StyleManager.ApplicationTheme = new Expression_DarkTheme();
        this.InitializeComponent();
    }
}
Run Code Online (Sandbox Code Playgroud)

要在运行时切换主题,您应该清除应用程序资源并添加新资源。

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)
    });
}
Run Code Online (Sandbox Code Playgroud)

您必须记住从安装文件夹中的 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.dllTelerik.Windows.Themes.Green.dll

请阅读以下文章以获取更多信息:

https://docs.telerik.com/devtools/wpf/styling-and-appearance/stylemanager/common-styling-apperance-setting-theme-wpf

https://docs.telerik.com/devtools/wpf/styling-and-appearance/how-to/styling-apperance-themes-runtime