我有一个包含复选框的列表视图。如果被检查我怎么能得到?
XAML:
<ListView Name="listview1" ItemsSource="{Binding UserCollection}">
<ListView.View>
<GridView>
<GridViewColumn Header="Discription" Width="170">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Discription}" Width="Auto"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Value" >
<GridViewColumn.CellTemplate>
<DataTemplate>
<StackPanel>
<CheckBox IsChecked="{Binding Path=Value}" Content="{Binding Path=Value}" Width="70" Name="ckBox1"/>
</StackPanel>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
当用户取消选中或检查复选框中的“值”复选框时,是否可以执行此操作?
ObservableCollection<UserData> _UserCollection = new ObservableCollection<UserData>();
public ObservableCollection<UserData> UserCollection
{
get { return _UserCollection; }
}
public class UserData
{
public string Discription { get; set; }
public bool Value { get; set; }
}
答案 0 :(得分:1)
您应该拥有UserData implement INotifyPropertyChanged,然后在更新时使Value属性通知。如果不采取这两个步骤,您的绑定将无法正常工作。完成这两件事之后,UserData的实例将包含复选框的值。
public class UserData : INotifyPropertyChanged
{
/* Sample code : missing the implentation for INotifyProperty changed */
private bool Value = true;
public bool Value
{
get{ return _value;}
set{ _value= value;
RaiseNotification("Value");
};
}
}
答案 1 :(得分:1)
ObservableCollection只是在集合内容发生更改时引发事件,而不是在其中一个UserData类的属性发生更改时引发事件。您可能需要考虑使UserData实现INotifyPropertyChanged。通过这种方式,您可以自动设置Value属性,并自动选中/取消选中UI绑定复选框。
public class UserData : INotifyPropertyChanged
{
private bool m_value;
public bool Value
{
get { return m_value; }
set
{
if (m_value == value)
return;
m_value = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}