簡體   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