我使用以下代码订阅了SelectionChangedEvent
a ComboBox
in a :DataGrid
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。通常,如果控件失去焦点,则会引发此问题...
此线程中找到的解决方案中的链接不再可用,我不知道如何使用此代码。