当前位置: 首页 > 使用教程

Telerik UI for WPF入门级教程:样式控件

发布时间:2022-01-12

由于提供的主题机制,在使用 NoXAML 二进制文件时自定义 Telerik 控件的外观和样式既简单又直观,要记住的一个重要步骤是始终将自定义样式基于主题的默认样式。

获取Telerik UI for WPF官方最新版

基于主题默认样式的自定义样式

创建针对特定控件的样式时,应基于主题提供的默认样式。 通过这种方式,您将确保所有属性(除了已更改的属性)都继承自默认样式。

我们遵循了一个命名约定,其中每个控件默认样式的键由控件名称后跟”Style” 后缀组成。 这意味着 RadGridView 控件的默认样式是 x:Key=”RadGridViewStyle”,RadComboBox 的默认样式是 x:Key=”RadComboBoxStyle” 等,示例 1 显示了如何基于 RadComboBox 控件的默认设置:

示例 1:RadComboBox 的自定义样式

XAML

<Style x:Key="CustomStyle" TargetType="telerik:RadComboBox" BasedOn="{StaticResource RadComboBoxStyle}">
<Setter Property="Foreground" Value="Green" />
</Style>

<Style TargetType="telerik:RadComboBox" BasedOn="{StaticResource RadComboBoxStyle}">
<Setter Property="Foreground" Value="Red" />
</Style>

注意Telerik主题还带有一些 MS 原生控件的样式,如 TextBox、CheckBox、Button 等。如果您想自定义这些控件的样式并保持 Telerik 主题,您可以使用与 Example1 相同的原则。 例如,TextBox 样式的 x:Key 是 TextBoxStyle。

样式自定义控件

请注意,自定义控件不会自动接收继承控件的样式,您必须将自定义样式基于继承控件的默认样式。

示例 2:为自定义控件添加样式

XAML

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Themes/System.Windows.xaml" />
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.xaml" />
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.GridView.xaml" />
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.Input.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style TargetType="local:MyCustomGrid" BasedOn="{StaticResource RadGridViewStyle}" />
</ResourceDictionary>
</Application.Resources>

然后您需要设置控件的 DefaultStyleKey。

示例 3:设置用于样式化自定义控件的 DefaultStyleKey

C#

public class MyCustomGrid : RadGridView
{
public MyCustomGrid()
{
this.DefaultStyleKey = typeof(MyCustomGrid);
}
}

VB.NET

Public Class MyCustomGrid
Inherits RadGridView
Public Sub New()
Me.DefaultStyleKey = GetType(MyCustomGrid)
End Sub
End Class
控件样式依赖项

示例 4 列出了与 Telerik 程序集对应的 XAML 文件以及它们之间的依赖关系。

注意:如果您使用 .NET Core,请注意必须使用 Telerik.Windows.Controls.RichTextBox.xaml 替代 Telerik.Windows.Documents.xaml 和 Telerik.Windows.Controls.RichTextBoxUI.xaml。

示例 4:与 Telerik 程序集对应的所有 XAML 文件

XAML

<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>

<!-- Telerik.Windows.Controls is used by all assemblies -->
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.xaml" />

<ResourceDictionary Source="Themes/System.Windows.xaml" />
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.Input.xaml" />

<!-- Requires: System.Windows.xaml -->
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.Navigation.xaml" />

<ResourceDictionary Source="Themes/Telerik.Windows.Controls.Chart.xaml" />

<!-- Requires: Telerik.Windows.Controls.Input -->
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.Data.xaml" />

<ResourceDictionary Source="Themes/Telerik.Windows.Controls.DataVisualization.xaml" />

<!-- Requires: Telerik.Windows.Controls.Navigation -->
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.Docking.xaml" />

<ResourceDictionary Source="Themes/Telerik.Windows.Controls.Expressions.xaml" />

<ResourceDictionary Source="Themes/Telerik.Windows.Controls.FixedDocumentViewers.xaml" />

<!-- Requires: Telerik.Windows.Controls.Input -->
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.GridView.xaml" />

<ResourceDictionary Source="Themes/Telerik.Windows.Controls.ImageEditor.xaml" />

<!-- Requires: Telerik.Windows.Controls.Input, Telerik.Windows.Controls.Navigation -->
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.RibbonView.xaml" />

<ResourceDictionary Source="Themes/Telerik.Windows.Controls.RichTextBoxUI.xaml" />

<!-- Requires: System.Windows, Telerik.Windows.Controls.Input, Telerik.Windows.Controls.Navigation -->
<ResourceDictionary Source="Themes/Telerik.Windows.Controls.ScheduleView.xaml" />

<!-- Requires: Telerik.Windows.Controls.Input, Telerik.Windows.Controls.Navigation -->
<ResourceDictionary Source="Themes/Telerik.Windows.Documents.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>

Telerik UI for WPF | 下载试用

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

更多产品正版授权详情及优惠,欢迎咨询在线客服>>


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

在线
客服
微信
QQ 电话
400-700-1020
返回
顶部