Search code examples
c#windows-phone-7silverlight-toolkitcaliburn.microlistpicker

caliburn.micro binding convention for ListPicker on WP7


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?


Solution

  • 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.