Note that there are some explanatory texts on larger screens.

plurals
  1. PODefault conversion fails when DatePicker.SelectedDate set to null
    text
    copied!<p>I'm binding a property of <code>DateTime?</code> to a DatePicker's SelectedDate and DisplayDate properties. It's behaving exactly as I'd like, but I notice this error in the VS Output window. </p> <pre><code>System.Windows.Data Error: 23 : Cannot convert '&lt;null&gt;' from type '&lt;null&gt;' to type 'System.DateTime' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: 'DateTimeValueSerializer' ValueSerializer cannot convert from '(null)'. at System.Windows.Markup.DateTimeValueSerializer.ConvertFromString(String value, IValueSerializerContext context) at System.Xaml.Replacements.DateTimeConverter2.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)' </code></pre> <p>I've been able to remove the error by converting <code>null</code> to <code>DateTime.Now</code> or to some other default date but that isn't the behavior I want - my DatePicker should show "Select a date" when the value is null, just like it does now.</p> <p>Should I make any special effort to eliminate this error?</p> <p>If so, how might I approach this?</p> <h2>Additional</h2> <p>The <code>DatePicker</code> in question is part of the <code>ControlTemplate</code> of a <code>ContentControl</code>. Here is a slightly distilled version of what I'm doing: </p> <pre><code>&lt;ContentControl Content="{Binding Path=MyDateProperty, Mode=TwoWay}"&gt; &lt;ContentControl.Template&gt; &lt;ControlTemplate TargetType="ContentControl"&gt; &lt;DatePicker DisplayDate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" SelectedDate="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"/&gt; &lt;/ControlTemplate&gt; &lt;/ContentControl.Template&gt; &lt;/ContentControl&gt; </code></pre> <p>In actual fact the template is selected and the bindings set in code, but this generates the errors I'm seeing.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload