简体   繁体   English

WPF ListView SelectedItem为null

[英]WPF ListView SelectedItem is null

I have a Listview that has a checkbox as one of the columns. 我有一个Listview,其中有一个复选框作为列之一。 If I click anywhere but the actual checkbox the SelectedItem of the ListView is set to the current selected row, as expected. 如果我点击实际复选框的任何位置,ListView的SelectedItem将按预期设置为当前选定的行。 If, on the other hand I click onto the checkbox (without clicking on the row first) then the SelectedItem is null or the previously clicked row. 另一方面,如果我单击复选框(不先单击该行),则SelectedItem为null或先前单击的行。

Can anyone help me out.... 谁能帮我吗....

Cheers 干杯

<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>

It's very easy, just handle Click event on your checkbox: 这很容易,只需处理复选框上的Click事件:

private void CheckBox_Click(object sender, RoutedEventArgs e) {
    var cb = sender as CheckBox;
    var item = cb.DataContext;
    myListView.SelectedItem = item;
}

You have to parse your visual tree to get the index of the checkbox that is checked and select that particular listbox item in your code whenever some checkbox is checked 您必须解析可视树以获取已选中复选框的索引,并在选中某个复选框时在代码中选择该特定列表框项

You may also be interested in 您也可能对。。。有兴趣

How to get checked items in a WPF ListBox? 如何获取WPF ListBox中的选中项?

and

http://goalbook.wordpress.com/2009/09/05/wpf-checkedlist-control/ http://goalbook.wordpress.com/2009/09/05/wpf-checkedlist-control/

Veer suggested parsing the visual tree to get the checkbox. Veer建议解析可视树以获取复选框。 The things is I already had the checkbox. 事情是我已经有了复选框。 What I needed was the listviewitem that held the checkbox. 我需要的是包含复选框的listviewitem。 After further research this blog post pointed me in the right direction. 经过进一步的研究,这篇博文指出了我正确的方向。 Here is the code to get the listviewitem of the row that the checkbox was clicked: 以下是获取单击复选框的行的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());
        }
    }

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

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