IsEnabled属性背景颜色在Wpf中未更改

时间:2012-06-22 11:43:11

标签: telerik wpf-controls

在我的WPF文本框中,当IsEnabled = false时,背景颜色为灰色..但是这个背景颜色没有设置在任何地方。当我设置

  <Trigger Property="IsEnabled" Value="false">
        <Setter Property="Background" Value="Blue"/>

它不起作用。当我在属性中更改IsEnabled =“true”时,我可以更改背景颜色。 任何人都可以解释为什么背景prorerty不适用于IsEnabled =“False”

1 个答案:

答案 0 :(得分:1)

实际上你需要更新TextBox模板中的Border背景。因此,您可以在触发器部分覆盖模板,并在那里设置适当的背景。

您可以找到所有默认的TextBox模板here

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBoxBase}">
    <Setter Property="SnapsToDevicePixels" Value="True"/>
    <Setter Property="OverridesDefaultStyle" Value="True"/>
    <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="MinWidth" Value="120"/>
    <Setter Property="MinHeight" Value="20"/>
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBoxBase}">
                <Border 
        Name="Border"
        CornerRadius="2" 
        Padding="2"
        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="Blue"/>
                        <Setter TargetName="Border" Property="BorderBrush" Value="{StaticResource DisabledBackgroundBrush}"/>
                        <Setter Property="Foreground" Value="{StaticResource DisabledForegroundBrush}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
相关问题