我有这个实体
public class StockQuantity : EntityBase
{
public virtual Part Part { get; set; }
public int Quantity { get; set; }
public int PartId { get; internal set; }
}
Part
public class Part : EntityBase
{
public string OEMNumber { get; set; }
public string BellNumber { get; set; }
public string Description { get; set; }
public string CountryOfOrigin { get; set; }
[Column(TypeName = "decimal(9, 3)")]
public double Weight { get; set; }
public virtual StockQuantity StockQuantity { get; set; }
public virtual ICollection<PartQuantity> PartQuantities { get; set; }
public virtual CountryCode CountryCode { get; set; }
}
我要绑定到DataGrid
<DataGrid ItemsSource="{Binding StockQuantities}"
SelectedItem="{Binding SelectedStockQuantity}"
x:Name="dataGridConsignments"
Margin="10,47,10,9.6"
ColumnWidth="*" AutoGenerateColumns="False" CanUserAddRows="True">
<i:Interaction.Triggers>
<i:EventTrigger EventName="CellEditEnding">
<i:InvokeCommandAction Command="{Binding UpdateQuantity}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn IsReadOnly="True" Header="Bell Number" Width="100" Binding="{Binding Part.BellNumber}"></DataGridTextColumn>
<DataGridTextColumn IsReadOnly="False" Header="Description" Binding="{Binding Part.Description}"></DataGridTextColumn>
<DataGridTextColumn IsReadOnly="False" Header="COO" Binding="{Binding Part.CountryCode.Code}"></DataGridTextColumn>
<DataGridTextColumn IsReadOnly="False" Header="Quantity" Width="100" Binding="{Binding Quantity,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ></DataGridTextColumn>
</DataGrid.Columns>
<DataGrid.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="{Binding RowDoubleClick}"/>
</DataGrid.InputBindings>
</DataGrid>
在我的ViewModel
中,我有以下命令,该命令在编辑单元格时会触发:
public ICommand UpdateQuantity
{
get
{
return updateQuantity
?? (updateQuantity = new ActionCommand(() =>
{
if (SelectedStockQuantity == null)
return;
var stockQ = AllStockQuantities.FirstOrDefault(sq => SelectedStockQuantity.Id == sq.Id);
if (stockQ == null)
return;
StandardDatabase.Commands.UpdateStockQuantity(stockQ.Part.Id, SelectedStockQuantity.Quantity, SelectedStockQuantity.Part.CountryCode.Code);
}));
}
}
问题是,当命令触发时,我可以看到Quantity
的更新值(这是StockQuantity
的根级属性),但是当我尝试更新CountryCode
时或Description
,则原始值仍存储在SelectedStockQuantity
中,因此不会在数据库中更新。
有人知道我可以在这种情况下强制SelectedStockQuantity
更新子对象属性吗?