WPF为什么指定的背景不能覆盖样式触发器中的setter值?

WPF为什么指定的背景不能覆盖样式触发器中的setter值?,wpf,triggers,styles,Wpf,Triggers,Styles,我有这样的文本框样式: <Style x:Key="TextBox_Standard" TargetType="{x:Type TextBoxBase}" > <Setter Property="Control.FontFamily" Value="/#Calibri" /> <Setter Property="Control.FontSize" Value="12" /> <Setter Property="Control.Ma

我有这样的文本框样式:

<Style x:Key="TextBox_Standard" TargetType="{x:Type TextBoxBase}" >
    <Setter Property="Control.FontFamily" Value="/#Calibri" />
    <Setter Property="Control.FontSize" Value="12" />
    <Setter Property="Control.Margin" Value="2" />
    <Setter Property="Control.Height" Value="21" />
    <Setter Property="Control.VerticalAlignment" Value="Center" />
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="UndoLimit" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type TextBoxBase}">
            <Border 
              Name="Border"
              CornerRadius="1" 
              Padding="1"
              Background="{StaticResource WindowBackgroundBrush}"
              BorderBrush="{StaticResource SolidBorderBrush}"
              BorderThickness="1" >
              <ScrollViewer Margin="0" x:Name="PART_ContentHost" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                    <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    <Setter Property="Cursor" Value="Arrow"/>
                </Trigger>
                <Trigger Property="IsReadOnly" Value="True">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                    <Setter Property="Focusable" Value="False"/>
                    <Setter Property="Cursor" Value="Arrow"/>
                </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
这是为了确保前景颜色为黑色,背景颜色为灰色时,它是不可编辑的

但显然,现在需要在背景不可编辑时以编程方式更改背景,因此我尝试如下:

<Style x:Key="TextBox_Standard" TargetType="{x:Type TextBoxBase}" >
    <Setter Property="Control.FontFamily" Value="/#Calibri" />
    <Setter Property="Control.FontSize" Value="12" />
    <Setter Property="Control.Margin" Value="2" />
    <Setter Property="Control.Height" Value="21" />
    <Setter Property="Control.VerticalAlignment" Value="Center" />
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="UndoLimit" Value="0"/>
    <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="{x:Type TextBoxBase}">
            <Border 
              Name="Border"
              CornerRadius="1" 
              Padding="1"
              Background="{StaticResource WindowBackgroundBrush}"
              BorderBrush="{StaticResource SolidBorderBrush}"
              BorderThickness="1" >
              <ScrollViewer Margin="0" x:Name="PART_ContentHost" />
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                    <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    <Setter Property="Cursor" Value="Arrow"/>
                </Trigger>
                <Trigger Property="IsReadOnly" Value="True">
                    <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                    <Setter Property="Focusable" Value="False"/>
                    <Setter Property="Cursor" Value="Arrow"/>
                </Trigger>
            </ControlTemplate.Triggers>
          </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
txtBox.Background=画笔.黄色

但实际情况是,它在可编辑时仍有白色背景,在不可编辑时仍有灰色背景

我哪里出错了?

试试这个

   <Style x:Key="TextBox_Standard" TargetType="{x:Type TextBoxBase}" >
        <Setter Property="Control.FontFamily" Value="/#Calibri" />
        <Setter Property="Control.FontSize" Value="12" />
        <Setter Property="Control.Margin" Value="2" />
        <Setter Property="Control.Height" Value="21" />
        <Setter Property="Control.VerticalAlignment" Value="Center" />
        <Setter Property="SnapsToDevicePixels" Value="True"/>
        <Setter Property="Background" Value="{StaticResource WindowBackgroundBrush}"></Setter>
        <Setter Property="UndoLimit" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TextBoxBase}">
                    <Border  
          Name="Border" 
          CornerRadius="1"  
          Padding="1" 
          Background="{TemplateBinding Background}" 
          BorderBrush="{StaticResource SolidBorderBrush}" 
          BorderThickness="1" >
                        <ScrollViewer Margin="0" x:Name="PART_ContentHost" />
                    </Border>
                    <ControlTemplate.Triggers>

                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                            <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                            <Setter Property="Cursor" Value="Arrow"/>
                        </Trigger>
                        <Trigger Property="IsReadOnly" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="{StaticResource DisabledBackgroundBrush}"/>
                            <Setter Property="Focusable" Value="False"/>
                            <Setter Property="Cursor" Value="Arrow"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

我看到它在可编辑模式下工作,不幸的是,当它不可编辑时,我实际上需要它是黄色的注:我是通过创建新样式来完成的,我只是想知道我是否可以通过修改当前的样式来完成,这样它就可以用于两种情况:1文本框A->可编辑-白色,不可编辑-灰色,2文本框B->可编辑-以编程方式,不可编辑-programmaticallyNo,您将无法在这两种情况下使用相同的样式,因为您使用的是触发器,每当IsReadonly或IsEnabled属性值更改时,它们将被执行。你可以创建一个在B中使用的新样式,而不需要任何触发器,并从该样式继承a的样式。啊,所以这不可能完成,呃,我会为它创建一个新样式,然后谢谢大家