【问题标题】:WPF Why the assigned background cannot override setter value in style triggers?WPF 为什么分配的背景不能覆盖样式触发器中的设置器值?
【发布时间】:2011-03-15 15:59:08
【问题描述】:

我有一个Style 用于TextBox,如下所示:

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

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

我哪里做错了?

【问题讨论】:

    标签: wpf triggers styles


    【解决方案1】:

    试试这个

       <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 -> 可编辑 - 以编程方式,不可编辑 - 以编程方式
    • 不,您不能在这两种情况下使用相同的样式,因为您使用的是触发器,只要 IsReadonly 或 IsEnabled 属性值发生更改,它们就会被执行。您可以创建一种用于 B 的新样式,无需任何触发器,并从该样式继承 A 的样式。
    • 啊啊啊啊啊啊啊啊啊T_T好吧我就为它创造一个新的风格然后谢谢大家
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-11
    • 2016-12-31
    • 2017-10-02
    • 2021-06-07
    • 2019-07-22
    相关资源
    最近更新 更多