简体   繁体   English

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

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

I'm trying to use: Modern UI for WPF and Extended WPF Toolkit , more specifically IntegerUpDown , and the problem is, the IntegerUpDown don't follow the selected ModernUI theme. 我正在尝试使用: WPFExtended WPF Toolkit的 Modern UI ,更具体地说是IntegerUpDown ,问题是IntegerUpDown没有遵循所选的ModernUI主题。 It's clearer when we changed it to dark theme, the IntegerUpDown stays with white background. 当我们将其更改为深色主题时,它更清晰,IntegerUpDown保持白色背景。

First, I try something like... 首先,我尝试类似...

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

...but in runtime I get the exception "Can only base on a Style with target type that is base type 'IntegerUpDown'." ...但是在运行时,出现异常“只能基于具有基本类型'IntegerUpDown'的目标类型的样式”。 I understand it, in the end IntegerUpDown is a "composite" control, not directly derived from Textbox... 我了解它,最终IntegerUpDown是一个“复合”控件,不是直接从Textbox派生的...

So, I don't know how I do. 所以,我不知道该怎么办。 In my research, I found that files that can be relevant: 在我的研究中,我发现可能相关的文件:

BUT I'm not a XAML expert to tie the 2 informations to get a solution OR if there is another some simple solution that I cannot see... 但是我不是XAML专家,不能将这两个信息联系起来以获得解决方案,或者如果还有其他一些我看不到的简单解决方案...

Thanks for any help. 谢谢你的帮助。

As you have noticed, when dealing with Custom Controls you'll often need to do some custom styling to reconfigure the layout. 如您所见,在处理自定义控件时,您通常需要做一些自定义样式来重新配置布局。 It all depends how far you want to take it, but to give you an idea I believe the workflow below should fix the backgroundcolor. 这一切都取决于您要使用它的程度,但是给您一个想法,我相信下面的工作流程应该可以修复backgroundcolor。

The backgroundcolor is bound to Xceed's resource themes, snippet from Wpf Toolkit : 背景色绑定到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>

The ModernUI Style (following the chosen theme) for these properties folows eg ModernUI TextBox : 这些属性的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>

The DynamicResources are found in the theme file, for example ModernUI.Dark.xaml 在主题文件中可以找到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" />

You can now hard code them in your Style, making them fixed on 1 theme 现在,您可以按照自己的样式对其进行硬编码,使其固定在1个主题上

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

For extensive styling you'll need to put in more work, for example this post adresses restyling the Watermark property. 要进行广泛的样式设计,您需要进行更多工作,例如, 该帖子介绍了重新设计Watermark属性的风格。

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

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