结合Telerik的WPF的主题与我的自定义样式
问题描述:
在我的WPF应用程序,我有一个组合框,改变主题被选择后改变:结合Telerik的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的当前主题风格(可以是绿色/ Office2016 /夏)
答
张贴在这里我从Telerik的团队得到了答案:
依存的名称将保持不变跨越不同的Telerik主题。因此,您只需使用单个资源名称即可。 要更新您的代码,以便它是工作时主题更改,请执行下列操作:
1 - 使用支持算法FMP =“{StaticResource的RadRadioButtonStyle}”
2 - 从静态资源改为DynamicResource你的风格分配