georgehatzi
georgehatzi

Reputation: 133

WPF DatePicker does not use Material Design's style

I am using MaterialDesignThemes in my WPF project and I am able to see MaterialDesign styles being used by various WPF controls I am using. In one particular situation, however, I'm having a great bit of difficulty trying to get a DatePicker to use the MaterialDesign styling. When I render DatePickers in a XAML view, it works as expected. The difference is that in the case that it does not work, the date picker is rendered in code behind. Below is my code. Can anyone help?

var dp = new DateControl()
{
     Margin = new Thickness(0, 0, _fontSize, 0),
     HorizontalAlignment = HorizontalAlignment.Left,
     Question = q,
     IsReadOnly = IsReadOnly || q.IsReadOnly
};
if (dataValue != null)
{
     DateTime dt = DateTime.MinValue;
     if (DateTime.TryParse(dataValue, out dt) && dt > DateTime.MinValue)
     {
           dp.SetSelectedDate((DateTime?)dt);
    }
    else
    {
         dp.Text = dataValue;
    }
}
if (!IsReadOnly)
    dp.AddHandler(DateControl.LostFocusEvent, new RoutedEventHandler(HandleUserResponse));

MaterialDesignThemes.Wpf.ColorZoneAssist.SetMode(dp, ColorZoneMode.PrimaryDark);

return dp;

Upvotes: 3

Views: 1949

Answers (1)

mm8
mm8

Reputation: 169420

You should set the Style property of your custom control:

dp.Style = FindResource("MaterialDesignDatePicker") as Style;

MaterialDesignThemes doesn't know what a DateControl is and won't automatically apply any style to such a control. The implicit style that is applied to DatePicker controls is not inherited.

Upvotes: 3

Related Questions