4

I'm trying the caliburn.micro framework for a new project but I'm stuck with binding a ListPicker (the one from the toolkit). When I change the control to a simple DropDown, everything works as expected. I assume that the DropDown is working correctly, because of the default convention implemented here:

AddElementConvention<Selector>(Selector.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
    .ApplyBinding = (viewModelType, path, property, element, convention) => {
        if (!SetBinding(viewModelType, path, property, element, convention))
            return false;

        ConfigureSelectedItem(element, Selector.SelectedItemProperty,viewModelType, path);
        ApplyItemTemplate((ItemsControl)element, property);

        return true;
    };

The ListPicker don't implement Selector, so I've tried to add a custom convention in my bootstrapper:

static void AddCustomConventions() {
    AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
        .ApplyBinding = (viewModelType, path, property, element, convention) => {
            ConventionManager.ConfigureSelectedItem(element, ListPicker.SelectedItemProperty,viewModelType, path);
            return true;
        };
}

Unfortunately, that don't work. Can you help?

MichaelS
  • 3,809
  • 2
  • 26
  • 33

1 Answers1

8

I solved my problem with this convention.

ConventionManager.AddElementConvention<ListPicker>(ListPicker.ItemsSourceProperty, "SelectedItem", "SelectionChanged")
    .ApplyBinding = (viewModelType, path, property, element, convention) =>
    {
        if (ConventionManager.GetElementConvention(typeof(ItemsControl)).ApplyBinding(viewModelType, path, property, element, convention))
        {
            ConventionManager.ConfigureSelectedItem(element, ListPicker.SelectedItemProperty, viewModelType, path);
            return true;
        }
        return false;
    };

Additionaly, there was another problem. My SelectedItem Property returned null but my Items Property didn't contained a null value. I got an exception, that the selected item is invalid, because it is not in the list.

MichaelS
  • 3,809
  • 2
  • 26
  • 33