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

发布于 2024-09-09 01:09:15 字数 2247 浏览 4 评论 0原文

我有一个 TextBoxStyle ,如下所示:

<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 = Brushes.Yellow; ,

但发生的情况是可编辑时它仍然具有白色背景不可编辑时为灰色背景。

我哪里做错了?

I have a Style for a TextBox like this:

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

It was made to make sure the foreground color to be black and background color to be gray when it's not editable.

but apparently now there's a requirement to change the background programmatically when it's not editable, so I tried it like this:

txtBox.Background = Brushes.Yellow;

but what happens is it still have a white background when editable and gray background when not editable.

Where did I go wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2024-09-16 01:09:15

试试这个

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

just try with this

   <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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文