简体   繁体   中英

Combine Telerik for WPF Theme with my custom style

In my WPF application, I have a combo-box that changes themes upon selection changed:

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;
        }
    }

And the switch theme methods look like this:

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();
    }

Same for SwitchToOffice2016Theme method:

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();
    }

Now the AddCommonResources method adds my own Resource Dictionary that is going to contain my own custom style:

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

    }

Now, in one of my views, I have a RadRadioButton as the following:

<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}"/>

Now what I'm trying to do is:

<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>

How do I achieve this based on behaviour? I mean, I don't have the Resource name like:

BasedOn="{StaticResource currentTelerikTheme}"

How can I achieve that? Tell WPF to be based on the current Theme style of Telerik (which can be Green/Office2016/Summer)

Posting here the answer I got from the Telerik team:

The dependency's name will remain the same across different Telerik themes. Therefore, you only need to use the single resource name. To update your code so that it it is works when theme changes, do the following:

1 - Use BasedOn="{StaticResource RadRadioButtonStyle}"

2 - Change your Style assignment from StaticResource to DynamicResource

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM