How to get Selected items in WPF CheckBox ListBox

前端 未结 3 1533
独厮守ぢ
独厮守ぢ 2020-12-21 23:13

Am Using the checkbox in listbox items, how to get the selected checkboxes from the list



        
3条回答
  •  一个人的身影
    2020-12-22 00:12

    You could move the data context for each of these items away from the UI and create an ObservableCollection of objects

    public ObservableCollection List { get;set;}
    
    public class CheckedItem : INotifyPropertyChanged
    {
      private bool selected;
      private string description;
    
      public bool Selected 
      { 
         get { return selected; }
         set 
         {
            selected = value;
            OnPropertyChanged("Selected");
         }
      }
    
      public string Description 
      { 
         get { return description; }
         set
         {
             description = value;
             OnPropertyChanged("Description");
         }
       }
    
      /* INotifyPropertyChanged implementation */
    }
    

    Then in your ListBox ItemTemplate

    
      
        
      
    
    

    Your selected items are now available in the ObservableCollection rather than looping through UI elements

提交回复
热议问题