WPF toolkit CheckListBox SelectedItemsOverride not working

后端 未结 3 1889
臣服心动
臣服心动 2021-01-22 13:50

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

相关标签:
3条回答
  • 2021-01-22 13:54

    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>();
    
    0 讨论(0)
  • 2021-01-22 14:06

    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
    }
    
    0 讨论(0)
  • 2021-01-22 14:16

    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
    
    0 讨论(0)
提交回复
热议问题