简体   繁体   English

如何检查listView行中选中的复选框

[英]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,其中我添加了一个带有checkBox的列,如下所示:

<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 请记住,ListView绑定到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 继承INotifyPropertyChanged

For example, what I use in most of my projects 例如,我在大多数项目中使用的内容

In XAML 在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 在您的代码中,您可以使用LINQ请求或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 您可以在MSDN上找到答案如何:使用CheckBox创建ListViewItems

The trick is to bind the IsChecked property of the Checkbox to the IsSelected property of the ListViewItem 诀窍是将Checkbox的IsChecked属性绑定到ListViewItem的IsSelected属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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