【发布时间】:2013-10-22 03:38:06
【问题描述】:
我在DataGrid 中的ComboBox 上使用以下代码订阅了SelectionChangedEvent:
public static DataGridTemplateColumn CreateComboboxColumn(string colName, Binding textBinding, SelectionChangedEventHandler selChangedHandler = null)
{
var cboColumn = new DataGridTemplateColumn {Header = colName};
...
if (selChangedHandler != null)
cboFactory.AddHandler(Selector.SelectionChangedEvent, selChangedHandler);
...
return cboColumn;
}
我实际注册的处理程序包含:
private void ComboBoxSelectionChangedHandler(object sender, SelectionChangedEventArgs e)
{
Console.WriteLine(@"selectHandler");
var cboBox = sender as ComboBox;
if (cboBox == null)
return;
if (cboBox.IsDropDownOpen) // a selection in combobox was made
{
CommitEdit();
}
else // trigger the combobox to show its list
cboBox.IsDropDownOpen = true;
}
...并且位于我的自定义 DataGrid 类中。
如果我在 ComboBox 中选择一个项目,e.AddedItems 和 cboBox.SelectedItem 包含所选值,但 CommitEdit() 上没有任何更改。
我想要的是在用户选择下拉列表中的项目时强制提交直接更新DataGrid 的ItemsSource。通常,如果控件失去焦点,则会引发此问题...
this thread 中找到的解决方案中的链接不再可用,我不知道如何使用此代码。
【问题讨论】: