Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF DatePicker Background Fix

Rate me:
Please Sign up or sign in to vote.
5.00/5 (8 votes)
6 Jun 2012CPOL 28.6K   402   9   2
A little trick that will more completely fill the DatePicker background from the code-behind.

WPF DatePicker Background Fix

There is an interesting issue with the DatePicker, and that is, for whatever reason Microsoft had, when you set the Background of the control, it only partially sets the Background. The center of the DatePicker remains a color of white. Not exactly what one would expect. Below is a picture, the top one is probably closer to what you would expect if you set the background to a gold color, the second textbox is what you actually get when you set the background to a gray.

The XAML for these controls looks like the following:

C#
<DatePicker Name="datePicker1" Background="#FFC000"/>
<DatePicker Name="datePicker2" Background="#A6A6A6" />

I found a little trick that will more completely fill the background from the code-behind. In the constructor of the code-behind, I added the following code for the first DatePicker control:

C#
datePicker1.Loaded += delegate
{
    var textBox1 = (TextBox)datePicker1.Template.FindName("PART_TextBox", datePicker1);
    textBox1.Background = datePicker1.Background;
};

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Clifford Nelson Consulting
United States United States
Has been working as a C# developer on contract for the last several years, including 3 years at Microsoft. Previously worked with Visual Basic and Microsoft Access VBA, and have developed code for Word, Excel and Outlook. Started working with WPF in 2007 when part of the Microsoft WPF team. For the last eight years has been working primarily as a senior WPF/C# and Silverlight/C# developer. Currently working as WPF developer with BioNano Genomics in San Diego, CA redesigning their UI for their camera system. he can be reached at qck1@hotmail.com.

Comments and Discussions

 
SuggestionXAML solution Pin
PierSilvio20-May-19 4:49
PierSilvio20-May-19 4:49 
QuestionDatepicker background Pin
RémidEB9-Apr-18 10:15
RémidEB9-Apr-18 10:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.