WPF ListView SelectedItem 为空 [英] WPF ListView SelectedItem is null

查看:35
本文介绍了WPF ListView SelectedItem 为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,其中有一个复选框作为列之一.如果我单击除实际复选框之外的任何位置,则 ListView 的 SelectedItem 会按预期设置为当前选定的行.另一方面,如果我单击复选框(没有先单击行),则 SelectedItem 为空或先前单击的行.

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.

谁能帮帮我....

干杯

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

推荐答案

很简单,只需处理复选框上的 Click 事件:

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

这篇关于WPF ListView SelectedItem 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆