• 界面组件Telerik UI for WPF入门指南 - 如何使用主题切换自定义样式


    获取Telerik UI for WPF官方最新版

    使用隐式样式设置主题,您可以选择在运行时更改控件的主题,而无需重新创建 UI。

    合并字典中的资源在资源锁定范围中占据一个位置,该位置正好在它们合并到的主资源字典的范围之后。 您可以做的是将自定义样式隔离在单独的资源字典中,并在每次更改主题时将它们添加到默认字典之后。

    例如,您可以按照以下步骤操作:

    1. 创建一个新应用程序并从位于Telerik UI for WPF安装文件夹中的 Binaries.NoXaml 文件夹中添加所需的程序集以及主题程序集:

    • Telerik.Windows.Controls.dll
    • Telerik.Windows.Controls.Input.dll
    • Telerik.Windows.Themes.Office_Black.dll
    • Telerik.Windows.Themes.Office2016.dll

    2. 在 App.xaml 中为默认主题添加相应的资源字典:

    示例 1:合并资源字典

    XAML

    1. <ResourceDictionary>
    2. <ResourceDictionary.MergedDictionaries>
    3. <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml"/>
    4. <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml"/>
    5. <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml"/>
    6. <!-- ... -->
    7. </ResourceDictionary.MergedDictionaries>
    8. </ResourceDictionary>

    注意:最初,我们合并 Office_Black 主题的资源字典。

    3. 将您选择的一些控件添加到应用程序的布局根目录中,还有两个用于在主题之间切换的按钮。

    示例 2:添加按钮来在主题之间切换

    XAML

    1. <Grid x:Name="LayoutRoot" Background="White">
    2. <Grid.RowDefinitions>
    3. <RowDefinition Height="*"/>
    4. <RowDefinition Height="auto"/>
    5. </Grid.RowDefinitions>
    6. <telerik:RadButton Content="Button" VerticalAlignment="Center" Width="100"/>
    7. <StackPanel Grid.Row="1" Orientation="Horizontal">
    8. <Button x:Name="Office_Black" Margin="5" Content="Office__Black" Click="Office_Black_Click"/>
    9. <Button x:Name="Office2016" Margin="5" Content="Office2016" Click="Office2016_Click"/>
    10. </StackPanel>
    11. </Grid>

    4. 现在,将自定义样式添加到项目的 Themes 文件夹中名为 CustomStyles_Office_Black.xaml 和 CustomStyles_Office2016.xaml的不同主题的单独资源字典中,这些自定义资源字典将具有以下内容:

    示例 3:在单独的资源字典中添加自定义样式

    XAML

    1. <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    2. xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
    3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    4. <Style TargetType="telerik:RadButton" BasedOn="{StaticResource RadButtonStyle}">
    5. <Setter Property="Background" Value="Black"/>
    6. <Setter Property="Foreground" Value="White"/>
    7. </Style>
    8. </ResourceDictionary>

    注意:为不同的主题创建单独的资源字典允许在每个主题的基础上轻松定制,但是您可以在切换主题时使用单个字典并仅合并此字典,前提是您没有任何特定于主题的更改。此类更改将包括任何修改过得空间模板,因为这些模板因主题而异,并且可能在切换时导致错误。

    示例 4:将字典添加到 MergedDictionaries

    XAML

    1. <ResourceDictionary.MergedDictionaries>
    2. <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/System.Windows.xaml"/>
    3. <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.xaml"/>
    4. <ResourceDictionary Source="/Telerik.Windows.Themes.Office_Black;component/Themes/Telerik.Windows.Controls.Input.xaml"/>
    5. <!-- ... -->
    6. <ResourceDictionary Source="/ProjectName;component/Themes/CustomStyles_Office_Black.xaml"/>
    7. </ResourceDictionary.MergedDictionaries>

    请注意,您应该将 ProjectName 替换为项目的实际名称。

    6. 然后,在按钮的 Click 处理程序中,我们将从应用程序资源中清除合并的字典,并将主题程序集中的新资源字典与自定义资源字典中包含的自定义样式合并:

    示例 5:单击按钮时清除和合并字典

    C#

    1. private void Office_Black_Click(object sender, RoutedEventArgs e)
    2. {
    3. this.MergeDictionaries("Office_Black");
    4. }
    5. private void Office2016_Click(object sender, RoutedEventArgs e)
    6. {
    7. this.MergeDictionaries("Office2016");
    8. }
    9. private void MergeDictionaries(string theme)
    10. {
    11. Application.Current.Resources.MergedDictionaries.Clear();
    12. Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
    13. {
    14. Source = new Uri("/Telerik.Windows.Themes." + theme + ";component/Themes/System.Windows.xaml", UriKind.RelativeOrAbsolute)
    15. });
    16. Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
    17. {
    18. Source = new Uri("/Telerik.Windows.Themes." + theme + ";component/Themes/Telerik.Windows.Controls.xaml", UriKind.RelativeOrAbsolute)
    19. });
    20. Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
    21. {
    22. Source = new Uri("/Telerik.Windows.Themes." + theme + ";component/Themes/Telerik.Windows.Controls.Input.xaml", UriKind.RelativeOrAbsolute)
    23. });
    24. Application.Current.Resources.MergedDictionaries.Add(new ResourceDictionary()
    25. {
    26. Source = new Uri("/ProjectName;component/Themes/CustomStyles_" + theme + ".xaml", UriKind.RelativeOrAbsolute)
    27. });
    28. }

    请注意,MergeDictionaries 中提供的主题应与相应主题程序集的名称匹配,例如 - Expression_Dark、Office2016Touch、Material。 使用这种方法,您可以切换到Telerik UI for WPF套件提供的任何主题。

    基于上述代码的结果如图 1 所示。

    图 1:带有 Office_Black 和 Office 2016 主题的单选按钮

    Telerik UI for WPF | 下载试用

    Telerik UI for WPF拥有超过100个控件来创建美观、高性能的桌面应用程序,同时还能快速构建企业级办公WPF应用程序。UI for WPF支持MVVM、触摸等,创建的应用程序可靠且结构良好,非常容易维护,其直观的API将无缝地集成Visual Studio工具箱中。


    Telerik_KendoUI产品技术交流群:726377843    欢迎一起进群讨论

    了解最新Kendo UI最新资讯,请关注Telerik中文网!

     

  • 相关阅读:
    C++ 二级指针与 const 关键字
    网络工程试验(一)链路聚合的三种情况
    SpringCloud Alibaba之Nacos Config 配置中心
    MySQL全库只读,不推荐使用set global readonly=true
    java SpringBoot基础
    json能够存储图片吗?
    java 之泛型详解
    vue 项目报Uncaught runtime errors: 导致项目崩溃
    MES管理系统中的质量管理活动是什么
    java计算机毕业设计springboot+vue青少年编程在线考试系统
  • 原文地址:https://blog.csdn.net/AABBbaby/article/details/125479659