【发布时间】:2018-09-02 08:50:44
【问题描述】:
我在尝试使用 DataGridComboBoxColumn 更新我的实体框架时遇到了一些问题
我有一个绑定到自定义模型 (FunctionPrinterLookupModel) 的数据网格,它基本上是建筑物周围打印机和函数之间的查找。功能都是静态的,但我希望用户能够选择他们用于该功能的打印机。
<DataGrid Grid.Row="1" x:Name="gridLookup" AutoGenerateColumns="False" Width="500" RowEditEnding="gridLookup_RowEditEnding" Margin="20">
<DataGrid.DataContext>
<Models:Printer/>
</DataGrid.DataContext>
<DataGrid.Columns>
<DataGridTextColumn Header="Function" Width="*" IsReadOnly="True" Binding="{Binding FunctionName}"/>
<!--<DataGridTextColumn Header="Printer" Width="*" Binding="{Binding PrinterName, UpdateSourceTrigger=PropertyChanged}"/>-->
<DataGridComboBoxColumn x:Name="ddlPrinters" Header="Printer" Width="*" SelectedValueBinding="{Binding PrinterID, Mode=TwoWay}" SelectedValuePath="{Binding PrinterID, Mode=TwoWay}" DisplayMemberPath="{Binding PrinterName}"/>
</DataGrid.Columns>
</DataGrid>
private void gridPrinters_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
Printer printer = (Printer)e.Row.Item;
if (printer.PrinterID != 0)
{
Printer printerDB = context.Printers.Where(s => s.PrinterID == printer.PrinterID).Single();
printerDB.PrinterName = printer.PrinterName;
context.SaveChanges();
}
else
{
Printer newPrinter = new Printer()
{
PrinterName = printer.PrinterName
};
context.Printers.Add(newPrinter);
context.SaveChanges();
}
}
RefreshPrintersGrid();
}
我将后面代码中的 DataGridComboBoxColumn 绑定到包含打印机列表的 EF 模型。
When the value has been selected and we trigger the RowEditEnding function, the value of the combobox is not updated in the FunctionPrinterLookupModel model.我觉得我在这里束手无策,并且无法从我数小时的谷歌搜索中找到有效的解决方案。任何人都可以帮我理顺吗?
【问题讨论】:
标签: wpf datagridcomboboxcolumn