Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WPF DatePicker Background Fix

0.00/5 (No votes)
6 Jun 2012 1  
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:

<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:

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here