Multiple selection in WPF MVVM ListBox

后端 未结 2 1096
粉色の甜心
粉色の甜心 2020-12-10 06:43

I have a ListBox containing filenames. Now I need to get array of selected items from this ListBox. I found a few answers here, but none of them worked for me. I\'am using C

2条回答
  •  时光取名叫无心
    2020-12-10 07:20

    You can use this code for MVVM Pattern

    XAML

    
        
            
                
            
        
        
            
        
    
    

    ViewModel

    private ObservableCollection deleteHistoryListBox = new ObservableCollection();
    public ObservableCollection DeleteHistoryListBox
    {
        get
        {
            return deleteHistoryListBox;
        }
        set
        {
            deleteHistoryListBox = value;
            this.RaisePropertyChanged("DeleteHistoryListBox");
        }
    }
    
    private HistoryItems deleteHistorySelectedItem;
    public HistoryItems DeleteHistorySelectedItem
    {
        get
        {
            return deleteHistorySelectedItem;
        }
        set
        {
            var selectedItems = DeleteHistoryListBox.Where(x => x.IsSelected).Count();
            this.RaisePropertyChanged("DeleteHistorySelectedItem");
        }
    }
    

    Class

    public class HistoryItems : INotifyPropertyChanged
    {
        private string item;
    
        public string Item
        {
            get { return item; }
            set
            {
                item = value;
                this.RaisePropertyChanged("Item");
            }
        }
    
        private bool isSelected;
    
        public bool IsSelected
        {
            get { return isSelected; }
            set
            {
                isSelected = value;
                this.RaisePropertyChanged("IsSelected");
            }
        }
    }
    

提交回复
热议问题