sturdytree
sturdytree

Reputation: 859

Make WPF Datepicker's foreground/background look like those in a Textbox when IsEnabled=False

When IsEnabled=false, the datepicker's text looks light grey whereas the textbox's text is a crisp black, which is what I want to acheive.

I have tried using the following, but no joy:

<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>

I'll be grateful for some pointers.

Upvotes: 0

Views: 3431

Answers (3)

Squirrel in training
Squirrel in training

Reputation: 788

I solved this binding to the IsEnabled Property and using a Converter:

XAML:

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

Converter-Methods:

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!");
}

Upvotes: 1

sturdytree
sturdytree

Reputation: 859

Following seems to work, in case it helps anyone else with similar issue:

<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>

Upvotes: 0

user2330678
user2330678

Reputation: 2311

Try

<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>

Upvotes: 0

Related Questions