【问题标题】:Update others cells in a DataGrid row after editing one编辑后更新 DataGrid 行中的其他单元格
【发布时间】:2018-02-09 01:44:52
【问题描述】:

我有这样的课:

public class Example
{
     public Example()
     {
     }

     public int Count1 { get; set; }

     public int Count2 { get; set; }

     public int Count3 { get { return Count1 + Count2 ; } }
}

还有一个带有List<Example> 作为 ItemSource 的 DataGrid:

<DataGrid x:Name="grdExample" AutoGenerateColumns="False" CanUserAddRows="False" SelectionMode="Single" >
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Count1 , Mode=TwoWay, UpdateSourceTrigger=LostFocus}"  Header="Count1" />
        <DataGridTextColumn Binding="{Binding Count1 , Mode=TwoWay, UpdateSourceTrigger=LostFocus}"  Header="Count1" />

        <DataGridTextColumn Binding="{Binding Count2 , Mode=TwoWay, UpdateSourceTrigger=LostFocus}"  Header="Count2" />
        <DataGridTextColumn Binding="{Binding Count3}"  Header="Count3" />

    </DataGrid.Columns>
</DataGrid>

我希望当我编辑 Count1 或 Count2 列时,Count3 也会更新。

使用此代码,仅更改了第二列(即与第一列相同的属性)。

我尝试在 SelectedCellsChanged 事件中刷新网格项目。但是会导致 StackOverFlow,一旦事件在每次刷新时不断变化。

我错过了什么?我正在寻找解决方案,但没有成功。

PS:调试我可以看到grdExample.Items中的值是正确的

【问题讨论】:

  • 我不知道您如何将数据绑定到数据网格,但您是否尝试过 UpdateSourceTrigger=PropertyChanged

标签: c# .net wpf datagrid updatesourcetrigger


【解决方案1】:

在您的模型类Example 中实现INotifyPropertyChanged 接口。每当 UI 中的值发生更改时,模型都会收到通知,并且在您的模型类中,您可以在 Count1Count2 更改时更新 Count3 值。

这个链接可以帮你实现INotifyPropertyChanged接口:https://docs.microsoft.com/en-us/dotnet/framework/winforms/how-to-implement-the-inotifypropertychanged-interface

【讨论】:

    猜你喜欢
    • 2014-12-13
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2015-08-24
    • 2018-12-06
    • 2013-09-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多