编辑后更新DataGrid [英] Update DataGrid after edited
问题描述
你好.
我正在使用DataGrid编辑和显示我的对象列表,如ItemsSource,List< TimeSegment> ;,我的对象如下所示:
Hello.
I''m using DataGrid to edit and display a list of my objects as ItemsSource, List<TimeSegment>, and my object looks like this:
public class TimeSegment
{
public DateTime StartTime
public DateTime EndTime
public TimeSpan Span
{
get
{
return this.EndTime - this.StartTime;
}
}
}
}
计算跨度属性.
但是,在DataGrid中编辑StartTime或EndTime之后,Span列并不会自动更新.而且我也想在编辑后对列表进行排序.
我对此有一个解决方法,但是并不能很好地解决这个问题.
这是我的解决方法,首先我在RowEditEnding事件中设置一个标志,然后我对ItemsSourceProperty进行ClearBinding,对列表进行排序,并在检查该标志后将其绑定到CurrentCellChanged事件中.
我的第一个问题是 有更好的方法吗?" .
如果必须使用此解决方法,那么我会遇到另一个问题.
我想在List< TimeSegment>之后执行一些逻辑改变了.编辑后,它可以正常工作,但不能删除行.
我的第二个问题是 如何删除行?" .
我尝试将逻辑放入DataGrid.DeleteCommand的Executed事件处理程序中,但这将覆盖DataGrid的默认操作,该行未删除,我的逻辑得到执行.
The Span property is calculated.
But the Span column did not update automatically, after I edit the StartTime or EndTime in DataGrid. And I want to sort the list after editing too.
I have a work around about this, but it dose not solve this gracefully.
Here is my work around, first I set a flag in the RowEditEnding event, then I ClearBinding for ItemsSourceProperty , sort the list and rebind the property in the CurrentCellChanged event after checking the flag.
My first question is "Is there a better way to do this?".
If I have to use this work around, then I got another problem.
I want to do some logic after the List<TimeSegment> changed. It works fine after editing, but not with deleting row(s).
My second question is "How can I do with deleting row(s)?".
I tried putting the logic in the event handler of Executed of DataGrid.DeleteCommand, but this will override the default action of DataGrid, the row did not deleted, my logic get executed.
<DataGrid.CommandBindings>
<CommandBinding Command="DataGrid.DeleteCommand"
Executed="CommandBinding_Executed" />
</DataGrid.CommandBindings>
有人可以帮我吗?在此先感谢您.
Can somebody help me? Thanks in advance.
推荐答案
HI在您的类中实现INotifiyPropertyChange
,并将您的datagrid与带有ObservableCollection<TimeSegment>
的id绑定;对象
HI implementINotifiyPropertyChange
in your class and bind your datagrid with an id with anObservableCollection<TimeSegment>
; object
这篇关于编辑后更新DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!