sturdytree sturdytree - 3 months ago 37
C# Question

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.

Answer Source

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>