You have to have a two-way binding on your SelectedItems
property. Either you do that explicitly in the binding expression like this:
<uc:CustomDataGrid ... SelectedItems="{Binding SelectedItems, Mode=TwoWay}"/>
or you set the FrameworkPropertyMetadataOptions.BindsTwoWayByDefault flags in the dependency property declaration:
public static readonly DependencyProperty SelectedItemsProperty =
DependencyProperty.Register(
"SelectedItems",
typeof(IEnumerable<ItemViewModel>),
typeof(CustomDataGrid),
new FrameworkPropertyMetadata(
null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault));
Note also that it is bad practice to set the default property value to a new collection instance, as that collection would be used as default value for all "instances" of the property. In other words, the default value of the SelectedItems
property on two instances of CustomDataGrid would be the same collection object. If you add items to the one, the items would also be contained in the other. You have to set the default value in the constructor of the control.
Edit: After taking a second look at your UserControl and how you bind its properties, I realized that it can't work the way you designed it. Setting the DataContext as done in your binding declaration
<uc:CustomDataGrid DataContext="{Binding Items}"
SelectedItems="{Binding SelectedItems}"/>
would require to explicitly set the binding source object of the SelectedItems
binding, perhaps like this
SelectedItems="{Binding SelectedItems, Source={StaticResource myViewModelInstance}}"
Instead of doing that, your control should have a bindable Items
or ItemsSource
in addition to the SelectedItems
property. You could then simply write your bindings like this:
<uc:CustomDataGrid DataContext="{StaticResource myViewModelInstance}"
ItemsSource="{Binding Items}" SelectedItems="{Binding SelectedItems}"/>