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



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

与 SwitchToOffice2016 主题方法相同:

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 的当前主题样式(可以是绿色/Office2016/夏季(

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

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

1 - use basedon="{StaticResource RadRadioButtonStyle}">

2 - 将样式分配从静态资源更改为动态资源

最新更新