简体   繁体   中英

how to check which checkbox is checked in listView rows

I have a ListView in which I added a column with a checkBox like so:

<ListView.View>
    <GridView>
        <GridViewColumn Width="60" Header="{x:Static properties:Resources.HistoryViewer_checkBoxRelaunch}" >
            <GridViewColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox Name="checkBoxRelaunch" Checked="checkBoxRelaunch_Checked" />
                </DataTemplate>
            </GridViewColumn.CellTemplate>
        </GridViewColumn>

the thing is that I need to be aware of which checkboxes were cheked by users, but I can't seem to get to that.

Bear in mind that the ListView is bound to a CollectionViewSource

thanks in advance.

The practice is to feed your collection with a class that

  • contains a property that will be linked to your check box.
  • inherits INotifyPropertyChanged

For example, what I use in most of my projects

In XAML

 <ListBox Grid.Row="1" Margin="5" MinHeight="200" Name="checkedListBoxControlFund" ScrollViewer.VerticalScrollBarVisibility="Auto" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <CheckBox  IsChecked="{Binding IsChecked}" Content="{Binding Label}" />
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

In your code

public class CheckBoxItemForWPF : IComparable<CheckBoxItemForWPF>, INotifyPropertyChanged
{
    public object Item { get; set; }

    private string _label;
    public string Label
    {
        get { return _label; }
        set { _label = value; OnPropertyChanged("Label"); }
    }

    private bool _isChecked;
    public bool IsChecked
    {
        get { return _isChecked; }
        set { _isChecked = value; OnPropertyChanged("IsChecked"); }
    }

    private SolidColorBrush _couleur;
    public SolidColorBrush Couleur
    {
        get { return _couleur; }
        set { _couleur = value; OnPropertyChanged("Couleur"); }
    }

    public CheckBoxItemForWPF(object item)
    {
        this.Item = item;
        this.Label = item.ToString();
        this.IsChecked = true;
        this.Couleur = new SolidColorBrush(Colors.White);
    }

    public CheckBoxItemForWPF(object item, string label)
    {
        this.Item = item;
        this.Label = label;
        this.IsChecked = true;
        this.Couleur = new SolidColorBrush(Colors.White);
    }

    public CheckBoxItemForWPF(string label, bool IsChecked)
    {
        this.Label = label;
        this.IsChecked = IsChecked;
        this.Couleur = new SolidColorBrush(Colors.White);
    }

    public CheckBoxItemForWPF(object item, string label, bool IsChecked)
    {
        this.Item = item;
        this.Label = label;
        this.IsChecked = IsChecked;
        this.Couleur = new SolidColorBrush(Colors.White);
    }

    public CheckBoxItemForWPF(object item, string label, bool IsChecked, SolidColorBrush Couleur)
    {
        this.Item = item;
        this.Label = label;
        this.IsChecked = IsChecked;
        this.Couleur = Couleur;
    }

    public override bool Equals(object obj)
    {
        // If parameter is null return false.
        if (obj == null)
        {
            return false;
        }

        // If parameter cannot be cast to Point return false.
        CheckBoxItemForWPF p = obj as CheckBoxItemForWPF;
        if ((System.Object)p == null)
        {
            return false;
        }

        return Item.Equals(p.Item);
    }

    public override int GetHashCode()
    {
        return Item.GetHashCode();
    }

    public override string ToString()
    {
        return Label;
    }

    public int CompareTo(CheckBoxItemForWPF other)
    {
        return this.Label.CompareTo(other.Label);
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged(string info)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(info));
        }
    }

}

In your code, you can retrieve checked elements with a LINQ request or a foreach

source : BindingList<CheckBoxItemForWPF> list
foreach (CheckBoxItemForWPF i in list)
   if (i.IsChecked)
      ...

You can find the answer at MSDN How to: Create ListViewItems with a CheckBox

The trick is to bind the IsChecked property of the Checkbox to the IsSelected property of the ListViewItem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM