Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change appearance of WPF DatePicker's textbox

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?

like image 394
Simon Baylis Avatar asked Feb 24 '17 11:02

Simon Baylis


2 Answers

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

like image 88
Ragavan Avatar answered Nov 10 '22 01:11

Ragavan


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?

No. You can't set any properties of an element that is defined inside a control's template (and that is not bound to any properties of the "parent" control itself) without re-defining the entire template or use implicit styles.

like image 39
mm8 Avatar answered Nov 09 '22 23:11

mm8