morexyoung 2019-12-20
解决办法:
在列的绑定属性里加上UpdateSourceTrigger,示例XAML如下
<DataGrid Name="dgProducts" IsReadOnly="False" CanUserAddRows="False" Grid.Row="1" CanUserDeleteRows="False" AutoGenerateColumns="False" Hyperlink.Click="dgProducts_Click"> <DataGrid.Columns> <DataGridTextColumn Header="产品编码" IsReadOnly="True" Binding="{Binding Path=ProductNO, Mode=TwoWay}"/> <DataGridTextColumn Header="产品名称" IsReadOnly="True" Width="150" Binding="{Binding Path=ProductName, Mode=TwoWay}"/> <DataGridTextColumn Header="规格型号" IsReadOnly="True" Width="150" Binding="{Binding Path=ProductSpec, Mode=TwoWay}"/> <DataGridTextColumn Header="生产数量" IsReadOnly="False" Binding="{Binding Path=MakeNums, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> <DataGridHyperlinkColumn Header="操作" Width="*" Binding="{Binding Path=PCmd}" /> </DataGrid.Columns> </DataGrid>这样当即时编辑Datagrid单元格数据时会立马更新数据源的值。
UpdateSourceTrigger 枚举详细说明见
http://msdn.microsoft.com/zh-cn/library/system.windows.data.updatesourcetrigger(v=vs.95).aspx