WPF 样式不适用于已设置样式的 UserControl

标签 wpf

我来找你是因为我在几个小时内对控制样式感到头疼。
通过为用户控件定义样式,它不起作用!

我的用户控件声明:

<uiComponent:NumericTextBox Text="{Binding myProperty}"/>

我想申请的风格:
<Style TargetType="uiComponent:NumericTextBox">
   <Setter Property="Background" Value="Black"/>
</Style>

为什么它不适用于 Background 属性,尽管它适用于 Visibility 属性!
我尝试了 TargetType=FrameworkElement,没有效果....

我的用户控件是一个数字文本框,它定义了自己的样式,如下所示:
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mwt="clr-namespace:Microsoft.Windows.Themes;assembly=PresentationFramework.Aero"
  xmlns:l="clr-namespace:LSX.Space.PropertyUI.NumericTextBox">

    <SolidColorBrush x:Key="CustomTextBox_Background" Color="White" />
    <SolidColorBrush x:Key="CustomTextBox_Foreground" Color="Black" />
    <LinearGradientBrush x:Key="CustomTextBox_Border" StartPoint="0,0" EndPoint="0,1">
        <GradientStop Color="#FFABADB3" Offset="0.05" />
        <GradientStop Color="#FFE2E3EA" Offset="0.07" />
        <GradientStop Color="#FFE3E9EF" Offset="1" />
    </LinearGradientBrush>

    <Style x:Key="{x:Type l:NumericTextBox}" TargetType="{x:Type l:NumericTextBox}">
        <Setter Property="Background" Value="{StaticResource CustomTextBox_Background}" />
        <Setter Property="BorderBrush" Value="{StaticResource CustomTextBox_Border}" />
        <Setter Property="Foreground" Value="{StaticResource CustomTextBox_Foreground}" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="SnapsToDevicePixels" Value="True" />
        <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type l:NumericTextBox}">
                    <Border x:Name="Border"
                      Background="{TemplateBinding Background}"
                      BorderBrush="{TemplateBinding BorderBrush}"
                      BorderThickness="{TemplateBinding BorderThickness}">
                        <Grid x:Name="LayoutGrid">
                            <ScrollViewer Margin="2" x:Name="PART_ContentHost" />
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <!--Message validation des erreurs-->
                        <MultiTrigger>
                            <MultiTrigger.Conditions>
                                <Condition Property="HasText" Value="True" />
                                <Condition Property="Validation.HasError" Value="True" />
                            </MultiTrigger.Conditions>
                            <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path= TextError}"/>
                            <Setter Property="Validation.ErrorTemplate">
                                <Setter.Value>
                                    <ControlTemplate>
                                        <DockPanel LastChildFill="True">
                                            <Image x:Name="ValidationIcon" DockPanel.Dock="Left" Stretch="None" Width="15" Height="15" Source="pack://application:,,,/LS.Net.Telcom.Space.PropertyUI;component/Images/validationError.png" />
                                            <Border BorderBrush="Red" BorderThickness="1">
                                                <AdornedElementPlaceholder />
                                            </Border>
                                        </DockPanel>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </MultiTrigger>

                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

非常感谢您的帮助。

最佳答案

我想我明白你在做什么...

您正在 ResourceDictionary 中设置样式,然后在其他地方再次设置样式。因此,您的 ResourceDictionary 正在加载您的样式中的背景,因此它会覆盖您在其他地方设置的内容。

这解释了为什么 Visibility 对您有用,因为该属性没有在您的 ResourceDictionary 的 Style 中设置。

您应该将 Style 设置为 StaticResource,然后基于该 Style 的任何后续样式。这基本上是 Sheridan 建议的,但您应该通过键名引用样式...

<Style x:Key="NumericBoxStyle" TargetType="{x:Type l:NumericTextBox}">
    <Setter Property="Background" Value="{StaticResource CustomTextBox_Background}" />
    <Setter Property="BorderBrush" Value="{StaticResource CustomTextBox_Border}" />
    <Setter Property="Foreground" Value="{StaticResource CustomTextBox_Foreground}" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="SnapsToDevicePixels" Value="True" />
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type l:NumericTextBox}">
                <Border x:Name="Border"
                  Background="{TemplateBinding Background}"
                  BorderBrush="{TemplateBinding BorderBrush}"
                  BorderThickness="{TemplateBinding BorderThickness}">
                    <Grid x:Name="LayoutGrid">
                        <ScrollViewer Margin="2" x:Name="PART_ContentHost" />
                    </Grid>
                </Border>
                <ControlTemplate.Triggers>
                    <!--Message validation des erreurs-->
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="HasText" Value="True" />
                            <Condition Property="Validation.HasError" Value="True" />
                        </MultiTrigger.Conditions>
                        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path= TextError}"/>
                        <Setter Property="Validation.ErrorTemplate">
                            <Setter.Value>
                                <ControlTemplate>
                                    <DockPanel LastChildFill="True">
                                        <Image x:Name="ValidationIcon" DockPanel.Dock="Left" Stretch="None" Width="15" Height="15" Source="pack://application:,,,/LS.Net.Telcom.Space.PropertyUI;component/Images/validationError.png" />
                                        <Border BorderBrush="Red" BorderThickness="1">
                                            <AdornedElementPlaceholder />
                                        </Border>
                                    </DockPanel>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </MultiTrigger>

                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

并调整样式...
<Style TargetType="uiComponent:NumericTextBox" BaseOn="{StaticResource NumericBoxStyle}">
   <Setter Property="Background" Value="Black"/>
</Style>

关于WPF 样式不适用于已设置样式的 UserControl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18145994/

相关文章:

c# - Treeview 每一级的不同绑定(bind)方式

wpf - Generic.xaml 有什么特别之处?

wpf - Visual Studio 和 WPF 应用程序 : High CPU usage when logged into other user

c# - 设置TabControl的背景颜色

c# - MVVM 通用 app.config

c# - WPF Datagrid 突出显示行和列

c# - 从初学者的角度来看使用 C# 学习 WPF 的好书

wpf - PowerShell WPF 数据网格 : Exception thrown committing empty row

c# - 如何跟踪 WPF 命令?

c# - 如何在 C# 的集合中保存不同类型的对象?