0

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

4

1 回答 1

0

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

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

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

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

于 2017-08-22T03:30:13.653 回答