Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 动态更改主题Telerik WPF_C#_Wpf_Telerik_Themes - Fatal编程技术网

C# 动态更改主题Telerik WPF

C# 动态更改主题Telerik WPF,c#,wpf,telerik,themes,C#,Wpf,Telerik,Themes,我需要允许用户在使用telerik WPF控件创建的应用程序中动态更改主题 我将绑定到XAML中的每个telerik控件,如下所示: XAML: telerik:StyleManager.Theme="{Binding SelectedSMTheme, Mode=TwoWay}" private Theme selectedSMTheme; public Theme SelectedSMTheme { get {

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

我将绑定到XAML中的每个telerik控件,如下所示:

XAML:

telerik:StyleManager.Theme="{Binding SelectedSMTheme, Mode=TwoWay}"
    private Theme selectedSMTheme;
    public Theme SelectedSMTheme
    {
        get
        {
            return selectedSMTheme;
        }
        set
        {
            selectedSMTheme = value;
            RaisePropertyChange("SelectedSMTheme");
        }
    }
SelectedSMTheme = new Expression_DarkTheme();
视图模型:

telerik:StyleManager.Theme="{Binding SelectedSMTheme, Mode=TwoWay}"
    private Theme selectedSMTheme;
    public Theme SelectedSMTheme
    {
        get
        {
            return selectedSMTheme;
        }
        set
        {
            selectedSMTheme = value;
            RaisePropertyChange("SelectedSMTheme");
        }
    }
SelectedSMTheme = new Expression_DarkTheme();
并在用户选择主题时更改此
SelectedSMTheme

改变主题:

telerik:StyleManager.Theme="{Binding SelectedSMTheme, Mode=TwoWay}"
    private Theme selectedSMTheme;
    public Theme SelectedSMTheme
    {
        get
        {
            return selectedSMTheme;
        }
        set
        {
            selectedSMTheme = value;
            RaisePropertyChange("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\u Dark.dll
    Telerik.Windows.Themes.Green.dll
有关更多信息,请阅读以下文章:


我使用的解决方案与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 support以1张支持票的形式提供给我。例如,您可以在文档中找到控件的大多数十六进制颜色。另外,请记住,某些WPF控件不受样式的影响(我认为TextBox就是一个例子),因此如果您使用的是香草WPF控件,它们可能会保持不变,并要求您硬编码新的颜色

多谢各位。但是那样的话,我必须重新运行我的应用程序,对吗?如果不关闭并重新打开它,我认为它将无法工作。所以,我在控制级别设置主题并对其进行更改。但是这样做,需要非常小心XAML,这看起来有点乏味,我正在寻找替代品…@UpendharSingirikonda请再次检查我的答案。再次感谢。但是,由于某些原因,我无法使用app.xaml,因为我正在将我的解决方案嵌入到另一个应用程序中。所以,我将尝试使用这个。资源。合并目录。。。除了向XAML中的每个控件添加主题的开销之外,与此方法相比,我最初的方法在稳定性和性能方面是否也很好?哪一种方法更稳定、可靠和性能更好?@UpendharSingirikonda我认为使用应用程序资源的解决方案是实现这一目标的正确方法。您的代码库将更加清晰,负责应用程序主题的代码将放在一个位置。好的,谢谢。但是,当我有时引用NoXAML DLL时,我的视图没有呈现,并且我遇到了一些问题,比如一些控件没有动态更改主题,等等。。。我会查的