dimanche 28 juin 2015

MultiSelectComboBox SelectedItems Binding

Currently working on MultiSelectComboBox custom control derived from combobox class, there is a listbox with multiselection mode inside the combobox template. What I need to do is to create a bindable selected items property for my combobox, so that it can bind to my view model in two way mode. But the thing is I am using list box to display the list of items, unfortunately SelectedItems property of listbox is read only, I can't just use SelectedItems={TemplateBinding SelectedItems}" in listbox. So now I able to get the selecteditems from viewmodel to my custom control but I need to update to the listbox so that it can display those items which are selected. Is there anyway to update my child control if the property has been updated, or any other solution to achieve this? Below is my code.

MultiSelectComboBox.xaml.cs

public static readonly DependencyProperty SelectedItemsProperty = 
        DependencyProperty.Register("SelectedItems", typeof(IEnumerable), typeof(MultiSelectComboBox));

public IEnumerable SelectedItems
{
    get { return (IEnumerable)GetValue(SelectedItemsProperty); }
    set { SetValue(SelectedItemsProperty, value); }
}

MainWindow.xaml

<usc:MultiSelectComboBox ItemsSource="{Binding Collection}"
                         SelectedItems="{Binding Selecteditems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

Selecteditems from viewmodel able to pass until MainWindow, just need one more step transfer to listbox to display as selected.

Aucun commentaire:

Enregistrer un commentaire