Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change BackgroundColor of DatePicker's inner Textbox

Tags:

c#

wpf

datepicker

What I want to do is to change the Background from the TextBox that is inside the DatePicker.

I tried to get it work by adding the <Style TargetType="{x:Type DatePickerTextBox}" but had no success because I already have other <Style> there.

Here is my <DatePicker>:

                    <DatePicker Grid.Column="1" Grid.Row="0"
                                VerticalContentAlignment="Center" MinHeight="20">
                       <DatePicker.SelectedDate>
                            <Binding Path="ViewModel.Value" UpdateSourceTrigger="PropertyChanged" 
                                     RelativeSource="{RelativeSource TemplatedParent}"/>
                        </DatePicker.SelectedDate>

                        <DatePicker.Style>
                            <Style TargetType="{x:Type DatePicker}">
                                <Setter Property="Background" Value="White"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                        Path=ViewModel.IsDirty}" Value="true">
                                        <Setter Property="Background" Value="LightYellow"/>
                                    </DataTrigger>
                                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource TemplatedParent}, 
                                        Path=ViewModel.IsInvalid}" Value="true">
                                        <Setter Property="BorderBrush" Value="Red"/>
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </DatePicker.Style>
                    </DatePicker>

So when the IsDirty is triggered the Backgroundgets LightYellow but the inner TextBox Background is still White.

How do I change this Background as well?

like image 232
SteveOhio Avatar asked Sep 07 '25 20:09

SteveOhio


1 Answers

In your Style, just add the desired DatePickerTextBox style as a resource:

<Style TargetType="{x:Type DatePicker}">
    <Style.Resources>
        <Style TargetType="DatePickerTextBox">
            <Setter Property="Background" Value="Transparent"/>
        </Style>
    </Style.Resources>
    <Setter Property="Background" Value="White"/>
    <Style.Triggers>
        <!-- your triggers here -->
    </Style.Triggers>
</Style>
like image 72
dymanoid Avatar answered Sep 09 '25 08:09

dymanoid