SortDescription和自动排序顺序刷新 [英] SortDescription and automatic sorted order refresh
问题描述
当我修改了在列表框中绑定的项目的值时,我预计排序顺序应该自动更改。
但是它不会。
我在这种情况下调用了.SortDescriptions.Clear()方法并重新分配一个SortDescription?
.Refresh()doesn
EDITED
我绑定并设置了这样的数据;
public Records myRecents;
....
// lbToday是一个ListBox。
// ModifiedTime是一个DateTime。
this.lbToday.ItemsSource = new ListCollectionView(myRecents);
this.lbToday.Items.SortDescriptions.Add(new SortDescription(ModifiedTime,ListSortDirection.Descending));
当应用程序第一次启动时,显示正确的结果。但是当我修改项目的值(在这种情况下为'ModifiedTime'属性)时,视图不会更改。我重新启动了应用程序,它再次显示正确的结果。
EDITED2
这里是记录
的源代码。
public class记录:ObservableCollection< RecordItem>
{
public Records(){}
}
这里是'RecordItem'的源代码
public class RecordItem:INotifyPropertyChanged
{
string queryString; public string QueryString {get {return queryString; } set {queryString = value;通知(QueryString); }}
DateTime modifiedTime; public DateTime ModifiedTime {get {return ModifiedTime; } set {modifiedTime = value;通知(修改时间);
public RecordItem(){}
public RecordItem(string qStr)
{
this.queryString = qStr;
this.modifiedTime = DateTime.Now;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName){if(this.PropertyChanged!= null){PropertyChanged(this,new PropertyChangedEventArgs(propName)); }}
}
注意 p>
当我在myRecents(Record类)中添加了一个项目时,它工作得很好。一个问题只发生在修改一个属性。
看看WPF博士的这篇文章: ItemsControl:'E'用于可编辑集合
它应该帮助您解决问题。
When I modified a value of a item which bound in listbox, I expected a sorted order should changing automatically.
But it doesn't.
Am I call a .SortDescriptions.Clear() method and reallocate a SortDescription in this case?
.Refresh() doesn't work.
EDITED
i bound and setted the data like this;
public Records myRecents;
....
//lbToday is a ListBox.
//ModifiedTime is a DateTime.
this.lbToday.ItemsSource = new ListCollectionView(myRecents);
this.lbToday.Items.SortDescriptions.Add(new SortDescription("ModifiedTime", ListSortDirection.Descending));
When the app launched in first time, it showed correct result. But when I modfying a value of item (in this case, 'ModifiedTime' property), a view doesn't changed. And I re-launched app, it showing a correct result again.
EDITED2
Here is a source code of the Records
.
public class Records : ObservableCollection<RecordItem>
{
public Records() { }
}
and here is a source code of the 'RecordItem'
public class RecordItem : INotifyPropertyChanged
{
string queryString; public string QueryString { get { return queryString; } set { queryString = value; Notify("QueryString"); } }
DateTime modifiedTime; public DateTime ModifiedTime { get { return modifiedTime; } set { modifiedTime = value; Notify("ModifiedTime"); } }
public RecordItem() { }
public RecordItem(string qStr)
{
this.queryString = qStr;
this.modifiedTime = DateTime.Now;
}
public event PropertyChangedEventHandler PropertyChanged;
protected void Notify(string propName) { if (this.PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propName)); } }
}
Note
When I Added a Item in myRecents, (the Record class), it works well. A problem has occured only modifying a property.
Take a look at this article from Dr. WPF: ItemsControl: 'E' is for Editable Collection
It should help you with your problem.
这篇关于SortDescription和自动排序顺序刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!