WPF ListView SelectedItem is null

时光毁灭记忆、已成空白 提交于 2019-12-17 12:59:13

问题


I have a Listview that has a checkbox as one of the columns. If I click anywhere but the actual checkbox the SelectedItem of the ListView is set to the current selected row, as expected. 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.

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>

回答1:


It's very easy, just handle Click event on your checkbox:

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



回答2:


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?

and

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




回答3:


Veer suggested parsing the visual tree to get the checkbox. The things is I already had the checkbox. What I needed was the listviewitem that held the checkbox. 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:

        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());
        }
    }


来源:https://stackoverflow.com/questions/2608187/wpf-listview-selecteditem-is-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!