C# 在组合框的SelectionChanged事件上更新DataGrid的ItemsSource

C# 在组合框的SelectionChanged事件上更新DataGrid的ItemsSource,c#,wpf,.net-4.0,datagrid,C#,Wpf,.net 4.0,Datagrid,我使用以下代码订阅了DataGrid中组合框上的SelectionChangedEvent: public static DataGridTemplateColumn CreateComboboxColumn(string colName, Binding textBinding, SelectionChangedEventHandler selChangedHandler = null) { var cboColumn = new DataGridTemplateColumn {Hea

我使用以下代码订阅了DataGrid中组合框上的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类中

如果我在组合框中选择了一个项目,则e.AddedItems和cboBox.SelectedItem包含所选的值,但CommittedIt上没有任何更改

我想要的是,当用户在下拉列表中选择一个项目时,强制提交直接更新DataGrid的ItemsSource。正常情况下,如果控件失去焦点,则会引发此问题


在中找到的解决方案中的链接不再可用,我不知道如何使用此代码。

我为我的问题创建了一个棘手但有效的解决方案。下面是上面修改的处理程序:

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
    {
        cboBox.Text = cboBox.SelectedValue as string;
        cboBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
    }
    else // user wants to open the combobox
        cboBox.IsDropDownOpen = true;
}
因为我的ComboBoxColumn是一个自定义DataGridTemplateColumn,所以当用户第一次选择单元格时,我强制它显示其列表

要更改绑定项的值,我手动用最近选择的项覆盖显示的文本,并强制UI选择另一项在本例中,右边的控件隐式调用CellEditEnding事件,在我的示例中,该事件提交整行:

private bool _isManualEditCommit = false;
private void _CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    // commit a manual edit
    // this if-clause prevents double execution of the EditEnding event
    if (!_isManualEditCommit)
    {   
        Console.WriteLine(@"_CellEditEnding() manualeditcommit");
        _isManualEditCommit = true;
        CommitEdit(DataGridEditingUnit.Row, true);
        _isManualEditCommit = false;
        checkRow(e.Row);
    }
}

也许我可以帮某人解决这个肮脏的问题-

我为我的问题创建了一个棘手但有效的解决方案。下面是上面修改的处理程序:

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
    {
        cboBox.Text = cboBox.SelectedValue as string;
        cboBox.MoveFocus(new TraversalRequest(FocusNavigationDirection.Right));
    }
    else // user wants to open the combobox
        cboBox.IsDropDownOpen = true;
}
因为我的ComboBoxColumn是一个自定义DataGridTemplateColumn,所以当用户第一次选择单元格时,我强制它显示其列表

要更改绑定项的值,我手动用最近选择的项覆盖显示的文本,并强制UI选择另一项在本例中,右边的控件隐式调用CellEditEnding事件,在我的示例中,该事件提交整行:

private bool _isManualEditCommit = false;
private void _CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
    // commit a manual edit
    // this if-clause prevents double execution of the EditEnding event
    if (!_isManualEditCommit)
    {   
        Console.WriteLine(@"_CellEditEnding() manualeditcommit");
        _isManualEditCommit = true;
        CommitEdit(DataGridEditingUnit.Row, true);
        _isManualEditCommit = false;
        checkRow(e.Row);
    }
}

也许我可以帮某人解决这个肮脏的问题-

对于在使用DataGridComboxColumn时遇到此问题的人,请注意,单独使用_CellEditEnding足以让它更新基础项;您不需要使用第一位代码。对于在使用DataGridComboxColumn时遇到此答案的人,请注意,单独使用_CellEditEnding足以使其更新基础项;您不需要使用第一位代码。