Change appearance of WPF DatePicker's textbox

Simon Baylis picture Simon Baylis · Feb 24, 2017 · Viewed 13.2k times · Source

Changing the Foreground property of the WPF DatePicker does change the colour of the text in the text box, but changing the Background property doesn't. But you can change it by styling the contained DatePickerTextBox. So I ended up with this:

<DatePicker Foreground="Yellow" >
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}" >
            <Setter Property="Background" Value="Black" />
        </Style>
    </DatePicker.Resources>
</DatePicker>

Is there a tidier way of doing this without templating the whole control? Is there a way to just template the named part i.e. PART_TextBox?

Answer

Ragavan picture Ragavan · Feb 24, 2017

You can change DatePickerTextBox style

Code

<DatePicker>
    <DatePicker.Resources>
        <Style TargetType="{x:Type DatePickerTextBox}">
            <Setter Property="Control.Template">
                <Setter.Value>
                    <ControlTemplate>
                        <TextBox x:Name="PART_TextBox" 
                                    Text="{Binding Path=SelectedDate, RelativeSource={RelativeSource AncestorType={x:Type DatePicker}}}" />
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DatePicker.Resources>
</DatePicker>

Please refer this link Custom WPF DatePickerTextBox Template Help