【问题标题】:WPF DataGrid ComboBox retrieving value before commitWPF DataGrid ComboBox在提交前检索值
【发布时间】:2012-10-03 12:18:53
【问题描述】:

我在 WPF 应用程序(使用实体框架)中有一个 DataGrid,其中有一个 ComboBox 作为其中的列之一。此ComboBox 绑定到一个数据源,该数据源使用对包含下拉列表中显示的名称的表的连接引用。它为此联接使用了一个 ID 字段(称为 SalesActMgrID)。我正在使用该表中特定名称的 List 填充下拉列表。

我的问题是,当从下拉列表中选择名称时,它会更改该连接表中的名称,而不是将 SalesActMgrID 更改为所选名称。

我想出了一种方法来更新我的数据源中的 ID,但我还没有想出一种方法来找出在下拉列表中选择的名称,以便我可以获得该名称的正确 ID。

组合列定义为:

               <DataGridComboBoxColumn 
                    SelectedItemBinding="{Binding Path=ClientContract.StaffRole_SalesActMgr.StaffName}" 
                    Header="Sales Act Mgr" 
                    x:Name="salesActMgrColumn" Width="Auto" >
                <DataGridComboBoxColumn.EditingElementStyle>
                    <Style TargetType="ComboBox">
                        <Setter Property="ItemsSource" 
                                Value="{Binding staffNamesListSAM}" />
                        <Setter Property="IsReadOnly"
                                Value="True" />
                    </Style>
                </DataGridComboBoxColumn.EditingElementStyle>
            </DataGridComboBoxColumn>

DataGrid 绑定到 EmployeeTime 的数据源。 表的完整连接是:

EmployeeTime.ClientContractID is joined to ClientContract.ClientContractID {M-1}
StaffRoles.StaffRoleID is joined to ClientContract.SalesActMgrID {1-M}

我正在使用以下代码逐个单元格地执行提交。

    private bool isManualEditCommit;

    private void consultantsDataGrid_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
    {
        if (!isManualEditCommit)
        {
            isManualEditCommit = true;
            string head = e.Column.Header.ToString();
            bool doCommit = true;

            switch (head)
            {
                case "Sales Act Mgr":
                    {
                        e.Cancel = true;
                        //This is where I have been able to 'hard code' in a different
                        //ID value into the SalesActMgrID field which then correctly
                        //updates the value, but I need to know what name was selected
                        //here so I can get the correct ID for that name and set it below.
                        ((EmployeeTime)e.EditingElement.DataContext).ClientContract.SalesActMgrID = 11;
                        doCommit = false;
                    }
                    break;
            }
            DataGrid grid = (DataGrid)sender;
            if (doCommit)
            {
                grid.CommitEdit(DataGridEditingUnit.Row, doCommit);
                EmployeeTime et = e.Row.Item as EmployeeTime;
                CreateBurdenValue(et);
            }
            else
            {
                grid.CancelEdit(DataGridEditingUnit.Row);
            }
            isManualEditCommit = false;
        }
    }
}

可能还有其他一些“更好”的方法可以做到这一点,我很想知道。至少,如果有人能指出我可以在任何提交操作完成之前获得所选名称的方向,我将不胜感激。

仅供参考,如果我让它通过并在单元格上执行正常的 CommitEdit,则所选名称实际上会在 StaffRole 表中更新,因此在网格中显示原始名称的每一行上,它们都会更改为新选择的名称(这不是我想要的)。

【问题讨论】:

  • 果然,这几天我一直在寻找这个问题的答案。在我发布这个之后,下次我继续搜索时,我终于找到了答案:(e.EditingElement as ComboBox).SelectionBoxItem

标签: c# wpf datagrid combobox commit


【解决方案1】:

我将总结一下 OP 在评论中发布的内容。要访问编辑处理程序中的选定项,请使用:

 (e.EditingElement as ComboBox).SelectionBoxItem

【讨论】:

    猜你喜欢
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-06-30
    • 2011-02-28
    • 2022-01-11
    • 2013-02-03
    • 2018-04-15
    相关资源
    最近更新 更多