我有一个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>
问题是我需要知道哪些复选框被用户嘲笑,但我似乎无法达到目的。
请记住,ListView绑定到CollectionViewSource
提前感谢。
答案 0 :(得分:2)
这种做法是为您的收藏提供一个
类例如,我在大部分项目中使用的内容
在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>
在您的代码中
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));
}
}
}
在您的代码中,您可以使用LINQ请求或foreach
检索已检查的元素source : BindingList<CheckBoxItemForWPF> list
foreach (CheckBoxItemForWPF i in list)
if (i.IsChecked)
...
答案 1 :(得分:0)
您可以在MSDN How to: Create ListViewItems with a CheckBox
找到答案诀窍是将Checkbox的IsChecked属性绑定到ListViewItem的IsSelected属性