c# - 动态更改主题 Telerik WPF

标签 c# wpf telerik themes

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

我在我的 XAML 中设置绑定(bind)到每个 telerik 控件,如下所示:

XAML:

telerik:StyleManager.Theme="{Binding SelectedSMTheme, Mode=TwoWay}"

View 模型:

    private Theme selectedSMTheme;
    public Theme SelectedSMTheme
    {
        get
        {
            return selectedSMTheme;
        }
        set
        {
            selectedSMTheme = value;
            RaisePropertyChange("SelectedSMTheme");
        }
    }

并在用户选择主题时更改此 SelectedSMTheme

改变主题:

SelectedSMTheme = new Expression_DarkTheme();

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

最佳答案

您可以使用 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.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

关于c# - 动态更改主题 Telerik WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52014419/

相关文章:

c# - 网络驱动器在启动时不可用

javascript - 项目是 unfiend 在 Kendo Grid 组中

javascript - 关闭子模态表单后父页面回发的问题

wpf - 为什么绑定(bind)更新而不实现 INotifyPropertyChanged?

telerik - 如何防止KendoGrid的RemoveRow函数提示?

c# - 使用二进制列作为比较标准

c# - 如何在设计 View 中为自定义控件添加边框?

c# - Azure AppService 设置的优先级?

c# - WPF 如何将 XML 与多个命名空间绑定(bind)

c# - 显示分层数据的 WPF 组合框