我是 wpf 的新手;我正在使用可编辑的组合框(用于搜索目的)。
当 ComboBox 中的文本发生更改时,搜索结果将显示在数据网格下方。When any row from the datagrid is selected its values are displayed in textboxes for editing.
当我在组合框中写一些东西时,相关的行会显示在数据网格中,但是当我单击选择一行时,应用程序会抛出一个nullreference exception
.
当 dataGrid 刷新逻辑在按钮单击之后时,我的应用程序可以正常工作。
dataGrid 的“SelectionChange”事件代码为:
private void CategoryRowSelected(object sender, System.Windows.Controls.SelectedCellsChangedEventArgs e)
{
ClearForm();
if(CategoryDataGrid.SelectedItem!=null)
{
categoryMember = CategoryDataGrid.SelectedItem as CategoryTbl; // value assigned to the object
// if (categoryMember != null)
CategoryName.Text = categoryMember.CategoryName; //Exception thrown on this statement
CategoryDescription.Text = categoryMember.CategoryDescription;
}
}
ComboBox 的 textChange 事件的代码是:
private void RefreshDataGrid(object sender, System.Windows.Controls.TextChangedEventArgs e)
{
CategoryDataGrid.SelectedIndex = -1;
//CategoryDataGrid.ItemsSource = RefreshQuery;
CategoryDataGrid.ItemsSource= Admin.RefreshCategoryDataGrid(NameCombo.Text);
}