如何使自定义控件遵循当前"主题/风格"在WPF上?

时间:2016-06-25 23:25:56

标签: wpf xaml modern-ui wpf-extended-toolkit

我试图使用: Modern UI for WPFExtended WPF Toolkit,更具体地说是IntegerUpDown,问题是,IntegerUpDown不遵循选定的ModernUI主题。当我们将其更改为黑暗主题时,它更清晰,IntegerUpDown保持白色背景。

首先,我尝试类似......

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

...但是在运行时我得到异常&#34;只能基于具有目标类型的Style 这是基本类型&#39; IntegerUpDown&#39;。&#34; 我理解它,最后IntegerUpDown是一个&#34;复合&#34;控制,不是直接从Textbox中派生出来的......

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

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

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

正如您所注意到的,在处理自定义控件时,您通常需要执行一些自定义样式来重新配置布局。这一切都取决于你想要走多远,但为了给你一个想法我相信下面的工作流程应该修复backgroundcolor。

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>

对于广泛的样式,您需要投入更多工作,例如this post 解决重印Watermark财产的问题。