wpfmvvmxceedchecklistbox

Unable to get SelectedItems through Xceed CheckListBox using MVVM


I have been using wpf xceed third party library for some of the UI components. I really like the way CheckListBox is being displayed at the screen. But I am not able to get the selectedItems bound to any property in the viewmodel (the setter never triggers). Here is the code -

I am using a dataprovider to get values from enum -

 <UserControl.Resources>
    <ObjectDataProvider MethodName="GetValues" ObjectType="{x:Type sys:Enum}" x:Key="DeviceClassDataProvider">
        <ObjectDataProvider.MethodParameters>
            <x:Type TypeName="Model:HANDeviceClass" />
        </ObjectDataProvider.MethodParameters>
    </ObjectDataProvider>

And then the control has been declared something like this -

<ext:CheckListBox Focusable="False" SelectedMemberPath="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}" SelectedItemsOverride="{Binding SelectedDeviceGroups, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding SelectedItem, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" Grid.RowSpan="7" Grid.Column="4" Padding="5" BorderThickness="0.8" BorderBrush="Gray" ItemsSource="{Binding Source={StaticResource DeviceClassDataProvider}}"/>

How will i get the selected items in it's viewmodel?

Any quick help would be highly appreciated!

Thanks in advance


Solution

  • Should work provided that SelectedDeviceGroups is a public property that returns an ICollection<HANDeviceClass>:

    public ICollection<HANDeviceClass> SelectedDeviceGroups { get; } = new ObservableCollection<HANDeviceClass>();
    

    XAML:

    <ext:CheckListBox ItemsSource="{Binding Source={StaticResource DeviceClassDataProvider}}"
                      SelectedItemsOverride="{Binding SelectedDeviceGroups}" />
    
    <TextBlock Text="{Binding SelectedDeviceGroups.Count}" />
    

    Items will be added to and removed from the source collection as you check and uncheck items respectively.