C# WPF ListBox Checkbox Binding IsChecked to a Field and IsSelected?

后端 未结 4 1267
天涯浪人
天涯浪人 2021-02-06 01:09

I\'m trying to bind a CheckBox to a field but also trigger the checkbox\'s IsSelected.

Here is the ListBox setup that is working with the Binding to data



        
4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-06 01:20

    Ok, I answered my own question (and there might better to do this so feel free to add) I added a Click event to the checkbox like so

    
     
        
          
          
     
    
    

    and then added this code for the Click Event

    private void CheckBox_Click(object sender, RoutedEventArgs e)
    {
        var cb = sender as CheckBox;
        var item = cb.DataContext;
        lstExclude.SelectedItem = item;
    }
    

    Now the item gets selected when you click the checkbox (checked or unchecked) and the item is available to the 'lstExclude.SelectedIndex' method

    I hope this helps anybody coming along with the same problem.

提交回复
热议问题