为什么NullReferenceException用于通过按钮单击事件选择列表框中存在的复选框?

时间:2022-09-17 10:49:40
 void y_Click(object sender, RoutedEventArgs e)
    {

        CheckBox che = lbox.SelectedItem as CheckBox;

        if ((bool)che.IsChecked)
        {
            MessageBox.Show(che.Content.ToString());
        }
    }

Here the button name is y and lbox is the listbox where I have added checkboxes dynamically.The button y is also added through Dynamically to the listbox.

这里的按钮名称为y,lbox是我已动态添加复选框的列表框。按钮y也通过动态添加到列表框中。

For button click event in the above code it says NullReferenceException and lbox.SelectedItem is Null as seen by breakpoint..If I implement the same code in lboxSelectionChanged event it works fine and lbox.SelectedItem is not null..

对于上面代码中的按钮单击事件,它说NullReferenceException和lbox.SelectedItem是断点所见的Null。如果我在lboxSelectionChanged事件中实现相同的代码,它工作正常,lbox.SelectedItem不为null。

What is wrong in my implementation?

我的实施有什么问题?

2 个解决方案

#1


3  

If the as operator fails to cast to your desired type, it returns null. Most likely what is happening here is that your lbox.SelectedItem is not a CheckBox, or it is null. You should check that che is null before attempting to get its IsChecked property.

如果as运算符无法转换为所需类型,则返回null。很可能这里发生的是你的lbox.SelectedItem不是CheckBox,或者它是null。在尝试获取其IsChecked属性之前,您应该检查che是否为null。

void y_Click(object sender, RoutedEventArgs e)
{

    CheckBox che = lbox.SelectedItem as CheckBox;

    if (che == null) return;    // <--- Add this

    if ((bool)che.IsChecked)
    {
        MessageBox.Show(che.Content.ToString());
    }
}

#2


0  

The thing actually is IsChecked is different from selectionchanged event of listbox.The checking of a checkbox doesn't mean that a listbox selection has been made. And the thing I found is that whenever I click on a checkbox content, the selection is changed but not when the checkbox is checked. So, that's the thing.

事实上IsChecked与listbox的selectionchanged事件不同。复选框的检查并不意味着已经进行了列表框选择。我发现的是,无论何时单击复选框内容,选中都会更改,但选中复选框时则不会更改。那就是那件事。

#1


3  

If the as operator fails to cast to your desired type, it returns null. Most likely what is happening here is that your lbox.SelectedItem is not a CheckBox, or it is null. You should check that che is null before attempting to get its IsChecked property.

如果as运算符无法转换为所需类型,则返回null。很可能这里发生的是你的lbox.SelectedItem不是CheckBox,或者它是null。在尝试获取其IsChecked属性之前,您应该检查che是否为null。

void y_Click(object sender, RoutedEventArgs e)
{

    CheckBox che = lbox.SelectedItem as CheckBox;

    if (che == null) return;    // <--- Add this

    if ((bool)che.IsChecked)
    {
        MessageBox.Show(che.Content.ToString());
    }
}

#2


0  

The thing actually is IsChecked is different from selectionchanged event of listbox.The checking of a checkbox doesn't mean that a listbox selection has been made. And the thing I found is that whenever I click on a checkbox content, the selection is changed but not when the checkbox is checked. So, that's the thing.

事实上IsChecked与listbox的selectionchanged事件不同。复选框的检查并不意味着已经进行了列表框选择。我发现的是,无论何时单击复选框内容,选中都会更改,但选中复选框时则不会更改。那就是那件事。