3

I've added a DatePicker in own of my views, and I get an error for the Visibility property (which I have not configured).

The error is:

System.Windows.Data Error: 'MS.Internal.Data.DynamicValueConverter' converter failed to convert value '8/18/1993' (type 'System.String'); BindingExpression: Path='DateOfBirth' DataItem='Gui.ViewModels.RegisterPersonalViewModel' (HashCode=64515557); target element is 'Microsoft.Phone.Controls.DatePicker' (Name='DateOfBirth'); target property is 'Visibility' (type 'System.Windows.Visibility').. System.ArgumentException: Requested value '8/18/1993' was not found.
   at System.Enum.EnumResult.SetFailure(ParseFailureKind failure, String failureMessageID, Object failureMessageFormatArgument)
   at System.Enum.TryParseEnum(Type enumType, String value, Boolean ignoreCase, EnumResult& parseResult)
   at System.Enum.Parse(Type enumType, String value, Boolean ignoreCase)
   at MS.Internal.SilverlightTypeConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.TargetDefaultValueConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at MS.Internal.Data.DynamicValueConverter.Convert(Object value, Type targetType, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertToTarget(Object value). 

The view field looks like:

<toolkit:DatePicker x:Name="DateOfBirth" />

And the property in the view model:

public DateTime DateOfBirth { get; set; }

Hence I have nothing which maps the Visibility property to the field. So why do it make that binding?

I also tried to add a visibility binding like this:

<toolkit:DatePicker x:Name="DateOfBirth"
                       Visibility="{Binding Path=IsDateOfBirthVisible,
                                    Converter={StaticResource BooleanToVisibilityConverter}}"/>

And add a field:

public DateTime DateOfBirth { get; set; }
public bool IsDateOfBirthVisible { get; set; }

But then I get the following exception:

An exception of type 'System.Exception' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary

I've also tried to add custom conventions:

ConventionManager.AddElementConvention<DatePicker>(DateTimePickerBase.ValueProperty, "Value", "SelectedDate");
ConventionManager.AddElementConvention<DatePicker>(DateTimePickerBase.ValueStringFormatProperty, "ValueStringFormat", "ValueStringFormatChanged");
ConventionManager.AddElementConvention<DatePicker>(DateTimePickerBase.ValueStringProperty, "ValueString", "ValueStringChanged");
ConventionManager.AddElementConvention<DatePicker>(UIElement.VisibilityProperty, "Visibility", "VisibilityChanged");

But that doesn't make a difference.

I'm new both to caliburn and windows phone, so there might be a stupid mistake somewhere.

How do I get rid of that exception?

(the datepicker is from https://phone.codeplex.com/)

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • from the error is seems that the binding for visibility is to the date item and not to boolean item, BindingExpression: Path='DateOfBirth' DataItem='Gui.ViewModels.RegisterPersonalViewModel' (HashCode=64515557); target element is 'Microsoft.Phone.Controls.DatePicker' (Name='DateOfBirth'); target property is 'Visibility' (type 'System.Windows.Visibility').. System.ArgumentException: Requested value '8/18/1993' was not found. – Bhupendra Aug 18 '13 at 10:56
  • @bjoshi: But why do it try to use that for the Visibility? – jgauffin Aug 18 '13 at 10:57
  • This is weird. Have you tried removing the `x:Name` property to see if the problem still exists, I mean not lean CM configure the binding automatically ? – Ibrahim Najjar Aug 18 '13 at 12:23

1 Answers1

2

Caliburn.Micro has a "catch all" convention definied on the FrameworkElement which binds your matching property on the viewmodel to the FrameworkElement.VisibilityProperty

AddElementConvention<FrameworkElement>(
    FrameworkElement.VisibilityProperty, "DataContext", "Loaded");

Because Caliburn.Micro does not know about the Microsoft.Phone.Controls.DatePicker type or its base classes (DateTimePickerBase, Control) it fallbacks to the FrameworkElement convention and it tries to bind your DateTime to the DatePicker VisibilityProperty so you see the exception in the Output window.

There are two solutions:

  • Write the binding by hand: <toolkit:DatePicker Value="{Bidning DateOfBirth}" />
  • Create a custom convention, which is in this case:

    ConventionManager.AddElementConvention<DatePicker>(
         DateTimePickerBase.ValueProperty, "Value", "SelectedDate");
    

    Note: you can only have one convetion for a given type, so this should be your only AddElementConvention<DatePicker> call.

nemesv
  • 138,284
  • 16
  • 416
  • 359