WPF 为什么指定的背景不能覆盖样式触发器中的setter值?
我有一个 TextBox
的 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 = 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个
just try with this