将Telerik for WPF Theme与我的自定义样式相结合

时间:2017-08-21 15:40:36

标签: c# wpf telerik styles basedon

在我的WPF应用程序中,我有一个组合框,可以在选择更改后更改主题:

private void OnThemeSelectionChanged(object sender, SelectionChangedEventArgs args)
    {
        var comboBox = sender as RadComboBox;
        if (sender == null) return;
        switch (comboBox.SelectedIndex)//@TODO - Turn to enum: 0 = Summer and etc 
        {
            case 0:
                SwitchToSummerTheme();
                break;
            case 1:
                SwitchToOffice2016Theme();
                break;
            case 2:
                SwitchToGreenTheme();
                break;
        }
    }

切换主题方法如下所示:

private void SwitchToGreenTheme()
    {
        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)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Green;component/Themes/Telerik.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute)
        });
        AddCommonResources();
    }

SwitchToOffice2016Theme方法相同:

private void SwitchToOffice2016Theme()
    {
        Application.Current.Resources.MergedDictionaries.Clear();
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.Navigation.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.GridView.xaml", UriKind.RelativeOrAbsolute)
        });
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("/Telerik.Windows.Themes.Office2016;component/Themes/Telerik.Windows.Controls.Data.xaml", UriKind.RelativeOrAbsolute)
        });
        AddCommonResources();
    }

现在AddCommonResources方法添加了我自己的资源字典,它将包含我自己的自定义样式:

 private void AddCommonResources()
    {
        Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
        {
            Source = new Uri("pack://application:,,,/Common;component/XamlResources/CustomResources.xaml", UriKind.RelativeOrAbsolute)
        });

    }

现在,在我的一个观点中,我有一个RadRadioButton如下:

<telerik:RadRadioButton GroupName="a" x:Name="btn_move"
 Command="{Binding OnMoveCommand}" Content="{DynamicResource MoveString}" 
Grid.Column="5" Margin="5,3" Style="{StaticResource btn_menu_RadRadio}"/>

现在我要做的是:

<Style x:Key="btn_menu_RadRadio" TargetType="{x:Type telerik:RadRadioButton}"
**BASED ON CURRENT THEME (GREEN/OFFICE2016/SUMMER)** >
    <Setter Property="Padding" Value="1" />
    <Setter Property="FontWeight" Value="SemiBold" />
    <Setter Property="FontSize" Value="20" />
</Style>

如何根据行为实现此目标?我的意思是,我没有资源名称,如:

BasedOn="{StaticResource currentTelerikTheme}"

我怎样才能实现这一目标?告诉WPF基于Telerik的当前主题风格(可以是Green / Office2016 / Summer)

1 个答案:

答案 0 :(得分:0)

在这里发布我从Telerik团队得到的答案:

  

在不同的Telerik主题中,依赖项的名称将保持不变。因此,您只需要使用单个资源名称。   要更新代码以使其在主题更改时起作用,请执行以下操作:

1 - 使用BasedOn =“{StaticResource RadRadioButtonStyle}”

2 - 将样式分配从StaticResource更改为DynamicResource

相关问题