我在WPF应用程序(带有Entity Framework)中有一个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表中得到更新,因此在显示原始名称的网格中的每一行上,它们都会更改为新的所选名称名称(这不是我想要的)。 (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 我将总结一下OP在评论中发布的内容。要访问编辑处理程序中的选定项目,请使用: (e.EditingElement as ComboBox).SelectionBoxItem (adsbygoogle = window.adsbygoogle || []).push({});
10-01 03:30