I have a check list box from the wpf toolkit 2. I cannot get all of the selected items. I read that I am supposed to use SelectedItemsOverride to get all of my selected items bu
I faced the same problem and solved it by assigning the private field of the property
In your example
private ObservableCollection<TestClass> _testClassSelected=new ObservableCollection<TestClass>();
I combined previous answers/comments and what worked for me was to bind SelectedItemsOverride
to an ObservableCollection<T>
, include UpdateSourceTrigger=PropertyChanged
, and attach a method to the ObservableCollection<T>
's event CollectionChanged
.
The XAML:
<xctk:CheckListBox
ItemsSource="{Binding AllItems}"
DisplayMemberPath="Display"
ValueMemberPath="Value"
SelectedItemsOverride="{Binding Path=DataContext.SelectedItems, RelativeSource={RelativeSource FindAncestor, AncestorType=Window}, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}">
</xctk:CheckListBox>
Code in ViewModel:
public IEnumerable<Item> AllItems { get; set; }
public ObservableCollection<Item> SelectedItems { get; set; }
public ViewModel()
{
SelectedItems = new ObservableCollection<Item>();
SelectedItems.CollectionChanged += SelectedItems_CollectionChanged;
}
private void SelectedItems_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Handle collection changed event
}
try add Mode and UpdateSourceTrigger at binding
<xctk:CheckListBox Name="MyCheckList"
ItemsSource="{Binding TestClassCollection}"
DisplayMemberPath="DisplayName"
SelectedItemsOverride="{Binding TestClassSelected,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
update: I'd checked example code from closed issue at Extended WPF Toolkit CodePlex site. Try to change TestClassSelected property to ObservableCollection. (Still keep UpdateSourceTrigger described above in .xaml)
public ObservableCollection<TestClass> TestClassSelected