开发者

WPF Why the assigned background cannot override setter value in style triggers?

开发者 https://www.devze.com 2023-01-07 06:09 出处:网络
I have a Style for a TextBox like this: <Style x:Key=\"TextBox_Standard\" TargetType=\"{x:Type TextBoxBase}\" >

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.

开发者_StackOverflow

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?


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

精彩评论

暂无评论...
验证码 换一张
取 消