DevExpress WPF新手入門教程 - 如何使用調色板資源

DevExpress主題允許您在代碼中使用調色板顏色作爲資源,您可以使用DevExpress主題顏色繪製自定義控件,以使應用程序的樣式保持一致。

DevExpress WPF v21.1高速下載

您可以將每個調色板顏色用作顏色 (PaletteColorThemeKey) 或畫筆 (PaletteBrushThemeKey)。

MainWindow.xaml

<ThemedWindow ...
xmlns:dxi="http://schemas.devexpress.com/winfx/2008/xaml/core/internal"
xmlns:dxt="http://schemas.devexpress.com/winfx/2008/xaml/themes">
<Button Background="{dxi:ThemeResource {dxt:PaletteBrushThemeKey ResourceKey=Button.Background}}"
BorderBrush="{dxi:ThemeResource {dxt:PaletteBrushThemeKey ResourceKey=Border}}"
BorderThickness="1" />
</ThemedWindow>

示例

下面的代碼示例演示瞭如何將VS2017Blue主題的Border調色板資源綁定到自定義控件的BorderBrush屬性:

CustomControls.cs

using System.Windows;
using System.Windows.Controls;

namespace WpfApp36 {
public class CustomControl1 : Control {
static CustomControl1() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomControl1), new FrameworkPropertyMetadata(typeof(CustomControl1)));
}

public string Text {
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register("Text", typeof(string), typeof(CustomControl1), new PropertyMetadata(null));
}
}

MainWindow.xaml

<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:local="clr-namespace:WpfApp36"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfApp36.MainWindow">
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
</Grid.RowDefinitions>
<local:CustomControl1 Grid.Row="0" Margin="5" Text="Custom control binding to a theme name"/>
</Grid>
</Window>

CustomControl1_Resources.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dxt="http://schemas.devexpress.com/winfx/2008/xaml/core/themekeys"
xmlns:local="clr-namespace:WpfApp36">
<Style TargetType="{x:Type local:CustomControl1}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:CustomControl1}">
<Border BorderBrush="{DynamicResource {dxt:PaletteBrushThemeKey ResourceKey=Border, ThemeName=VS2017Blue}}" BorderThickness="3">
<TextBlock Foreground="Black" HorizontalAlignment="Center" Text="{TemplateBinding Text}" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>

App.xaml

<Application x:Class="WpfApp36.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="/WPFApp36;component/CustomControl1_Resources.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>

添加 XPF.Core 和 VS2017Blue 主題引用來運行這個項目。

DevExpress WPF入門級教程 - 如何使用調色板資源

調色板顏色列表

此部分包含調色板顏色名稱和值。

您可以在以下文件中找到主題資源鍵:

DevExpressControlsInstallationPath\Components\Sources\DevExpress.Xpf.Themes\Theme_Name\Core\Core\ Themes\Theme_Name\Palettes.xaml DevExpressControlsInstallationPath\Components\Sources\DevExpress.Xpf.Themes\Theme_Name\Core\Core\ Themes\Theme_Name\Palettes_Base.xaml

DevExpress WPF | 下載試用

DevExpress WPF擁有120+個控件和庫,將幫助您交付滿足甚至超出企業需求的高性能業務應用程序。通過DevExpress WPF能創建有着強大互動功能的XAML基礎應用程序,這些應用程序專注於當代客戶的需求和構建未來新一代支持觸摸的解決方案。 無論是Office辦公軟件的衍伸產品,還是以數據爲中心的商業智能產品,都能通過DevExpress WPF控件來實現。


DevExpress技術交流羣4:715863792      歡迎一起進羣討論

更多DevExpress線上公開課、中文教程資訊請上中文網獲取

慧都DevExpress 8月公開課火熱報名中
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章