【问题标题】:Combine Telerik for WPF Theme with my custom style将 Telerik for WPF 主题与我的自定义样式相结合
【发布时间】:2017-08-21 15:40:35
【问题描述】:

在我的 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)
        });

    }

现在,在我的一个观点中,我有一个 RadioButton,如下所示:

<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 当前的 Theme 样式(可以是 Green/Office2016/Summer)

【问题讨论】:

    标签: c# wpf telerik styles basedon


    【解决方案1】:

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

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

    1 - 使用 BasedOn="{StaticResource RadioRadioButtonStyle}"

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-09
      • 2015-10-04
      • 1970-01-01
      相关资源
      最近更新 更多