当IsEnabled = False时,使WPF Datepicker的前景/背景看起来像文本框中的那些

时间:2014-07-01 23:55:29

标签: c# wpf datepicker

当IsEnabled = false时,datepicker的文本看起来是浅灰色,而文本框的文本是一个清脆的黑色,这就是我想要实现的目标。

我尝试使用以下内容,但没有快乐:

<Style TargetType="{x:Type DatePickerTextBox}">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="LightGray"></Setter>
            <Setter Property="FontWeight" Value="Normal"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

我会感激一些指示。

3 个答案:

答案 0 :(得分:1)

我解决了这个绑定到IsEnabled属性并使用转换器:

<强> XAML:

<Style TargetType="DatePickerTextBox">
    <Setter Property="Background" Value="{Binding IsEnabled, Mode=OneWay, RelativeSource={RelativeSource Self}, Converter={StaticResource BoolToBackgroundConverter"/>
</Style>

<强>转换器的方法:

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    if ((bool)value)
        return System.Windows.Media.Brushes.White;
    else
        return System.Windows.Media.Brushes.WhiteSmoke;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
    throw new NotImplementedException("One way conversions only!");
}

答案 1 :(得分:0)

尝试

<Style TargetType="{x:Type DatePickerTextBox}">
    <Style.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter Property="Background" Value="LightGray"></Setter>
            **<Setter Property="FontWeight" Value="Black"></Setter>**
        </Trigger>
    </Style.Triggers>
</Style>

答案 2 :(得分:0)

以下似乎有效,以防其他人遇到类似问题:

<Style TargetType="{x:Type customControls:DatePicker}">
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate>
                            <Border BorderBrush="LightGray" BorderThickness="1">
                            <TextBlock Margin="4,0,0,0" Text="{Binding Path=Text, StringFormat={}{0:d}, RelativeSource={RelativeSource TemplatedParent}}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Trigger>
        </Style.Triggers>
    </Style>