繁体   English   中英

如何使自定义控件遵循WPF上当前的“主题/样式”?

[英]How to make a custom control follow the current “theme/style” on WPF?

我正在尝试使用: WPFExtended WPF Toolkit的 Modern UI ,更具体地说是IntegerUpDown ,问题是IntegerUpDown没有遵循所选的ModernUI主题。 当我们将其更改为深色主题时,它更清晰,IntegerUpDown保持白色背景。

首先,我尝试类似...

<Style TargetType="{x:Type local:IntegerUpDown}" BasedOn="{StaticResource {x:Type TextBox}}" />

...但是在运行时,出现异常“只能基于具有基本类型'IntegerUpDown'的目标类型的样式”。 我了解它,最终IntegerUpDown是一个“复合”控件,不是直接从Textbox派生的...

所以,我不知道该怎么办。 在我的研究中,我发现可能相关的文件:

但是我不是XAML专家,不能将这两个信息联系起来以获得解决方案,或者如果还有其他一些我看不到的简单解决方案...

谢谢你的帮助。

如您所见,在处理自定义控件时,您通常需要做一些自定义样式来重新配置布局。 这一切都取决于您要使用它的程度,但是给您一个想法,我相信下面的工作流程应该可以修复backgroundcolor。

背景色绑定到Xceed的资源主题,即Wpf Toolkit的片段:

<Style x:Key="NumericUpDown" TargetType="{x:Type prim:InputBase}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.WindowTextBrushKey}}" />
        <Setter Property="Background" Value="{DynamicResource {x:Static themes:ResourceKeys.ControlNormalBackgroundKey}}" />
        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static themes:ResourceKeys.ControlNormalBorderKey}}" />
</Style>

这些属性的ModernUI样式(遵循所选主题)如下,例如ModernUI TextBox

<Style TargetType="{x:Type TextBoxBase}">
        <Setter Property="Foreground" Value="{DynamicResource InputText}"/>
        <Setter Property="Background" Value="{DynamicResource InputBackground}"/>
        <Setter Property="BorderBrush" Value="{DynamicResource InputBorder}"/>
</Style>

在主题文件中可以找到DynamicResources,例如ModernUI.Dark.xaml

<SolidColorBrush x:Key="InputText" Color="#d1d1d1" />

<SolidColorBrush x:Key="InputBackground" Color="#333333" />
<SolidColorBrush x:Key="InputBackgroundHover" Color="#3e3e42" />
<SolidColorBrush x:Key="InputBorder" Color="#333333" />

现在,您可以按照自己的样式对其进行硬编码,使其固定在1个主题上

<Style TargetType="{x:Type local:IntegerUpDown}">
      <Setter Property="Foreground" Value="#d1d1d1" />
      <Setter Property="Background" Value="#333333" />
      <Setter Property="BorderBrush" Value="#333333" />
</Style>

要进行广泛的样式设计,您需要进行更多工作,例如, 该帖子介绍了重新设计Watermark属性的风格。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM