【问题标题】:WPF DataGrid does not display cell edits if I switch the selected row如果我切换选定的行,WPF DataGrid 不显示单元格编辑
【发布时间】:2015-02-21 06:09:18
【问题描述】:

我有一个 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
}

这是怎么回事?

【问题讨论】:

  • 在更改单元格值时,您是否还更改了与数据网格绑定的itemsource Items
  • 不,我不会那样做。
  • 这可能是原因。您的数据网格与Itemsource绑定,并且datagrid要求Itemsource被chaged。
  • 好的,我去看看。
  • 我很确定,这将解决您的问题。 ..

标签: c# wpf events mvvm datagrid


【解决方案1】:

我想通了。

问题是我用十进制值进行了测试。所以SomeValue 绑定到decimal

Binding="{Binding Path=SomeValue ...

但是StringFormat错了

Binding="{Binding Path=SomeValue, ..., StringFormat=d, ...}" />

两者都 - 删除 StringFormat 部分或将其更改为

StringFormat={}{0:0.00}

解决了这个问题。我之前没有注意到该列的值从未被填充。

【讨论】:

    猜你喜欢
    • 2015-11-07
    • 2019-02-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2012-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多