如何使用调色板资源
DevExpress主题允许在代码中使用调色板颜色作为资源,您可以用DevExpress主题颜色绘制自定义控件来使应用程序的样式保持一致。
您可以使用每种调色板颜色作为颜色(PaletteColorThemeKey)或画笔(PaletteBrushThemeKey)。
<ThemedWindow ... xmlns:dxi="//schemas.devexpress.com/winfx/2008/xaml/core/internal" xmlns:dxt="//schemas.devexpress.com/winfx/2008/xaml/core/themekeys"> <Grid> <Button Background="{dxi:ThemeResource {dxt:PaletteBrushThemeKey ResourceKey=Button.Background}}" BorderBrush="{dxi:ThemeResource {dxt:PaletteBrushThemeKey ResourceKey=Border}}" BorderThickness="1" /> </Grid> </ThemedWindow>
示例
下面的代码示例演示了如何将VS2017Blue主题的边界调色板资源绑定到自定义控件的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="//schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:local="clr-namespace:WpfApp36" xmlns:x="//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_Resource.xaml:
<ResourceDictionary xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="//schemas.microsoft.com/winfx/2006/xaml" xmlns:dxt="//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="//schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="//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主题引用来运行这个项目。
调色板颜色列表
此部分包含调色板颜色名称和值。
您可以在以下文件中找到主题资源键:
DevExpressControlsInstallationPath\Components\Sources\XPF\DevExpress.Xpf.Themes\Theme_Name\Core\Core\ Themes\Theme_Name\Palettes.xaml
DevExpressControlsInstallationPath\Components\Sources\XPF\DevExpress.Xpf.Themes\Theme_Name\Core\Core\ Themes\Theme_Name\Palettes_Base.xaml
Win11Dark:
Win11Light
Win10Dark
Win10Light
Office2019Black
Office2019Colorful
Office2019DarkGray
Office2019HighContrast
Office2019White
VS2019Blue
VS2019Dark
VS2019Light
VS2017Blue
VS2017Dark
VS2017Light
Office2016BlackSE
Office2016ColorfulSE
Office2016WhiteSE