我有一个Listview,其中有一个复选框作为其中一列。如果我点击实际复选框的任何位置,ListView的SelectedItem将按预期设置为当前选定的行。另一方面,如果我单击复选框(不先单击该行),则SelectedItem为null或先前单击的行。
任何人都可以帮助我....
干杯
<ListView Width="auto" SelectionMode="Single" x:Name="listBox" ItemsSource="{Binding MyData}" SelectedItem="{Binding Path=SelectedMyData}">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="120">
<GridViewColumn.CellTemplate>
<DataTemplate>
<ContentPresenter Style="{StaticResource DateTimeContent}" Content="{Binding MyDate}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Is Correct" Width="100">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="False"
Checked="OnChkChecked"
Unchecked="OnChkChecked"
IsChecked="{Binding IsCorrect}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="False"
Checked="OnChkChecked"
Unchecked="OnChkChecked"
IsChecked="{Binding IsCorrect}"></CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
答案 0 :(得分:16)
这很容易,只需处理复选框上的Click事件:
private void CheckBox_Click(object sender, RoutedEventArgs e) {
var cb = sender as CheckBox;
var item = cb.DataContext;
myListView.SelectedItem = item;
}
答案 1 :(得分:3)
您必须解析可视树以获取已选中复选框的索引,并在选中某个复选框时在代码中选择该特定列表框项
您可能也对
感兴趣How to get checked items in a WPF ListBox?
和
http://goalbook.wordpress.com/2009/09/05/wpf-checkedlist-control/
答案 2 :(得分:0)
Veer建议解析可视树以获取复选框。事情是我已经有了复选框。我需要的是包含复选框的listviewitem。经过进一步研究,blog post指出了我正确的方向。以下是获取单击复选框的行的listviewitem的代码:
private void chkbox_Checked(object sender, RoutedEventArgs e)
{
DependencyObject dep = e.OriginalSource as DependencyObject;
while ((dep != null) && !(dep is ListViewItem))
{
dep = VisualTreeHelper.GetParent(dep);
}
if (dep != null)
{
IMyViewModel vm = DataContext as IMyViewModel;
vm.SelectedThing = (MyListItemViewModel)lst.ItemContainerGenerator.ItemFromContainer(dep);
vm.DoSomethingCommand.Execute(e.RoutedEvent.Name.ToLower());
}
}