我是新手wpf;我正在使用可编辑的comboBox(用于搜索目的)。
当ComboBox中的文本更改时,搜索结果将显示在datagrid下方。选择数据表中的任何行后,其值将显示在文本框中进行编辑。
当我在combobox中写入某些内容时,相关行将显示在数据网格中,但是当我单击以选择行时,应用程序会抛出nullreference异常。
当数据网格刷新逻辑位于按钮单击之后时,我的应用程序可以正常工作。
数据网格的“SelectionChange”事件代码如下:
当ComboBox中的文本更改时,搜索结果将显示在datagrid下方。选择数据表中的任何行后,其值将显示在文本框中进行编辑。
当我在combobox中写入某些内容时,相关行将显示在数据网格中,但是当我单击以选择行时,应用程序会抛出nullreference异常。
当数据网格刷新逻辑位于按钮单击之后时,我的应用程序可以正常工作。
数据网格的“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);
}
CategoryDataGrid.SelectedItem
强制转换为(CategoryTbl)
,那么使用as
有何意义呢? - Conrad FrixNullReferenceException
)而不是一个无效转换异常(InvalidCastException
)。 - Reed Copsey