我即將ObservableCollection在代碼隱藏(而不是前端)中對 EF Core 檢索到的資料庫進行排序。
這是模型的類:
public class BindDesignModel : INotifyPropertyChanged
{
[Key]
public string id { get; set; }
string _code;
public string code
{
get => _code; set
{
_code = value;
OnPropertyChanged();
}
}
public string DesignFileName { get; set; }
string _name;
[NotMapped]
public string name
{
get => _name;
set
{
_name = value;
OnPropertyChanged();
}
}
[NotMapped]
public Boolean IsBinded => !string.IsNullOrEmpty(code);
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
該屬性IsBinded僅用于對ObservableCollection.
這是代碼
Database.Context Context = new Database.Context();
CollectionView CV = new CollectionView(Context.BindDesign.Local.ToObservableCollection());
BindDesignIC.ItemsSource = CV;
CV.SortDescriptions.Add(new SortDescription("IsBinded", ListSortDirection.Ascending));
這BindDesignIC是一個ItemsControl,如果直接系結它,它可以運行而沒有任何錯誤ObservableCollection。
但是,上面的代碼運行后,它報告了這個錯誤:
System.NotSupportedException
HResult=0x80131515
訊息=不支持指定的方法。
源=WindowsBaseStackTrace:
在 System.ComponentModel.SortDescriptionCollection.EmptySortDescriptionCollection.InsertItem(Int32 索引,SortDescription 項)
我的代碼有什么問題?謝謝你。
uj5u.com熱心網友回復:
不要顯式創建一個CollectionView.
WPF 為您創建一個,您可以使用以下CollectionViewSource.GetDefaultView方法獲取對它的參考:
dg.ItemsSource = Context.BindDesign.Local.ToObservableCollection();
ICollectionView CV = CollectionViewSource.GetDefaultView(dg.ItemsSource);
CV.SortDescriptions.Add(new SortDescription("IsBinded", ListSortDirection.Ascending));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/496865.html
上一篇:當我嘗試在WPF中更改ItemsControl的寬度時出現UI性能問題
下一篇:Wpf4.8系結到靜態屬性
