如果我切换所选行,WPF DataGrid 不显示单元格编辑
WPF DataGrid does not display cell edits if I switch the selected row
我有一个 WPF DataGrid,我通过双击编辑一个单元格,这通常有效。
但是如果我在一个单元格中处于编辑模式,然后用鼠标单击不同的行,那么编辑将不会存储在该单元格中。它是空的。
但我用鼠标单击 同一行中的另一个单元格 - 单元格值将正确显示。
<DataGrid ItemsSource="{Binding Items, Mode=TwoWay}"
SelectionMode="Single"
CanUserAddRows="False"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
CellEditEnding="DataGrid_OnCellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="Test"
Binding="{Binding Path=SomeValue, Mode=TwoWay, ValidatesOnExceptions=True, StringFormat=d, TargetNullValue={x:Static sys:String.Empty}}" />
</DataGrid.Columns>
</DataGrid>
代码隐藏
private void DataGrid_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
//save the changes in the DB
}
这是怎么回事?
我想通了。
问题是我用十进制值测试的。所以 SomeValue
绑定到 decimal
.
Binding="{Binding Path=SomeValue ...
但是 StringFormat 是错误的
Binding="{Binding Path=SomeValue, ..., StringFormat=d, ...}" />
两者 - 删除 StringFormat
部分或将其更改为
StringFormat={}{0:0.00}
已解决问题。之前没注意到列值从来没有被填充过
我有一个 WPF DataGrid,我通过双击编辑一个单元格,这通常有效。
但是如果我在一个单元格中处于编辑模式,然后用鼠标单击不同的行,那么编辑将不会存储在该单元格中。它是空的。
但我用鼠标单击 同一行中的另一个单元格 - 单元格值将正确显示。
<DataGrid ItemsSource="{Binding Items, Mode=TwoWay}"
SelectionMode="Single"
CanUserAddRows="False"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
CellEditEnding="DataGrid_OnCellEditEnding">
<DataGrid.Columns>
<DataGridTextColumn Header="Test"
Binding="{Binding Path=SomeValue, Mode=TwoWay, ValidatesOnExceptions=True, StringFormat=d, TargetNullValue={x:Static sys:String.Empty}}" />
</DataGrid.Columns>
</DataGrid>
代码隐藏
private void DataGrid_OnCellEditEnding(object sender, DataGridCellEditEndingEventArgs e) {
//save the changes in the DB
}
这是怎么回事?
我想通了。
问题是我用十进制值测试的。所以 SomeValue
绑定到 decimal
.
Binding="{Binding Path=SomeValue ...
但是 StringFormat 是错误的
Binding="{Binding Path=SomeValue, ..., StringFormat=d, ...}" />
两者 - 删除 StringFormat
部分或将其更改为
StringFormat={}{0:0.00}
已解决问题。之前没注意到列值从来没有被填充过