简体   繁体   English

在ComboBox的SelectionChanged事件上更新DataGrid的ItemsSource

[英]Update DataGrid's ItemsSource on SelectionChanged event of a ComboBox

I subscribed to a SelectionChangedEvent on a ComboBox in a DataGrid with the following code: 我使用以下代码订阅了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;
}

The handler I actually register contains: 我实际注册的处理程序包含:

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;
}

... and is located in my custom DataGrid class. ...并且位于我的自定义DataGrid类中。

If I select an item in the ComboBox, e.AddedItems and cboBox.SelectedItem contains the selected value, but nothing is changed on CommitEdit() . 如果我在ComboBox中选择一个项目,则e.AddedItemscboBox.SelectedItem包含选定的值,但CommitEdit()上没有任何更改。

What I want is to force a commit to directly update the DataGrid 's ItemsSource, when the user selects an item in the drop-down-list. 我想要的是当用户在下拉列表中选择一个项目时,强制一次提交直接更新DataGrid的ItemsSource。 Normally this is raised if the control looses focus... 通常,如果控件失去焦点,则会引发此事件。

The link in the solution found in this thread is not available any more and I don't know how to use this code. 线程中解决方案中的链接不再可用,我不知道如何使用此代码。

I created a tricky, but working, solution for my problem. 我为问题创建了一个棘手但可行的解决方案。 Here's the modified handler from above: 这是上面的修改后的处理程序:

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;
}

Because my ComboBoxColumn is a custom DataGridTemplateColumn I force it to show its list, when the user first selects the cell. 因为我的ComboBoxColumn是自定义的DataGridTemplateColumn所以我在用户第一次选择该单元格时强制其显示其列表。

To change the bound items value I manually overwrite the displayed text with recently selected item and force the UI to select another item (in this case the control to the right) to make an implicit call to CellEditEnding event, which (in my case) commits the whole row: 要更改绑定项的值,我手动用最近选择的项覆盖显示的文本,并强制UI选择另一个项(在本例中为右边的控件)对CellEditEnding事件进行隐式调用,该事件(以我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);
    }
}

Maybe I could help somebody with this "dirty" solution ;-) 也许我可以用这个“肮脏的”解决方案来帮助别人;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM