禁用DatePickerTextBox白色背景

时间:2012-03-07 10:02:11

标签: wpf datepicker

以下是我对WPF DatePicker的样式:

<Style TargetType="{x:Type DatePickerTextBox}">
    <Setter Property="Padding" Value="1" />
    <Setter Property="Background" Value="Transparent" />
    <Setter Property="BorderBrush" Value="Transparent" />
    <Setter Property="BorderThickness" Value="0" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="AllowDrop" Value="true"/>
    <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
    <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
    <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
    <Setter Property="OverridesDefaultStyle" Value="True" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DatePickerTextBox}">
                <Border
                    x:Name="brdBackground"
                    Padding="{TemplateBinding Padding}"
                    Background="{TemplateBinding Background}"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                    <ScrollViewer
                        x:Name="PART_ContentHost"
                        Foreground="{TemplateBinding Foreground}"
                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter TargetName="brdBackground" Property="Opacity" Value="0.5" />
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<Style TargetType="{x:Type DatePicker}">
    <Setter Property="Margin" Value="6" />
    <Setter Property="Background" Value="#858ca0" />
    <Setter Property="BorderBrush" Value="#303e66" />
    <Setter Property="BorderThickness" Value="1" />
    <Setter Property="Foreground" Value="Black" />
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Opacity" Value="0.5" />
        </Trigger>
    </Style.Triggers>
</Style>

启用DatePicker时,它看起来完全符合我的要求:

http://imageshack.us/photo/my-images/819/enabled.jpg/

但是,当禁用DatePicker时,其中包含的DatePickerTextBox的背景将变为白色:

http://imageshack.us/photo/my-images/832/disableda.jpg/

当DatePicker被禁用时,我不希望DatePickerTextBox以任何方式更改。当它的IsEnabled属性设置为false时,DatePicker的不透明度设置为0.5,这就是我想要发生的一切。鉴于我正在使用控件模板并且我正在覆盖DatePickerTextBox的默认样式,我开始认为它实际上并不是将DatePickerTextBox设置为白色而是其他东西。任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

它不会变白,它实际上是半透明白色(A5FFFFFF): - )

来源:http://msdn.microsoft.com/en-us/library/ff468215.aspx

现在就是你的情况,你复制/粘贴的模板不是你的DatePicker中的模板,它实际上是它包含的DatePickerTextBox中的模板。但灰色不是在那里完成的,它是在DatePicker本身完成的,特别是在这里:

<VisualState x:Name="Disabled">
    <Storyboard>
        <DoubleAnimation Duration="0"
                         To="1"
                         Storyboard.TargetProperty="Opacity"
                         Storyboard.TargetName="PART_DisabledVisual" />
    </Storyboard>
</VisualState>

要防止这种情况,您必须编辑DatePicker的模板并更改以下内容之一:

  • PART_DisabledVisual元素的背景颜色为透明
  • 或从模板中删除上面代码中显示的StoryBoard。