【发布时间】: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;
但是发生的情况是它在可编辑时仍然具有白色背景,而在不可编辑时仍然具有灰色背景。
我哪里做错了?
【问题讨论】: