[英]How to refresh Devexpress GridControl ItemsSource(as Datatable) in MVVM(WPF)
[英]Devexpress GridControl - refresh data in MVVM
我的網格:
<dxg:GridControl x:Name="StatisticsGridLevel1"
dx:ThemeManager.ThemeName="Office2013"
DataContext="{Binding FooViewModel}"
ItemsSource="{Binding FooCollection}">
視圖模型:
private List<FooDto> fooCollection = new List<FooDto>();
public List<FooDto> FooCollection
{
get
{
return this.fooCollection;
}
private set
{
this.fooCollection = value;
this.NotifyPropertyChanged();
}
}
和示例方法:
private void Foo()
{
foreach (var element in collection)
{
this.fooCollection.Add(new FooDto()
{
X = element.Foo1,
Y = element.Foo2,
Z = element.Foo3
});
}
this.NotifyPropertyChanged("FooCollection");
}
當我使用ObservableCollection時,一切正常。 但是我想使用列表(不是在循環中通知)。
在網格上開始滾動后,視圖將刷新。 問題是什么?
我認為CollectionViewSource
可以在您的情況下工作。 有很多方法可以在XAML中,在ViewModel中以及在View的代碼隱藏中創建一個。 我將把最簡單的一個放在一起進行演示,這是在ViewModel上創建CollectionViewSource
屬性。 我認為有些人可能不一定喜歡這種方法-有點讓人擔憂。 不過,我不確定我是否同意。 如果您認為CollectionViewSource
是集合視圖的對象模型,那么我認為ViewModel中沒有任何問題。 但我認為,由於它繼承自DependencyObject
因此受到了更多的關注,因此受到了恥辱。 無論如何,這樣的事情會做你想要的:
// Assuming this is your constructor
public ViewModel()
{
this.FooViewSource.Source = this.fooCollection;
}
private readonly List<FooDto> fooCollection = new List<FooDto>();
private readonly CollectionViewSource fooViewSource;
public CollectionViewSource FooViewSource
{
get { return this.fooViewSource; }
}
private void Foo()
{
foreach (var element in collection)
{
this.fooCollection.Add(new FooDto()
{
X = element.Foo1,
Y = element.Foo2,
Z = element.Foo3
});
}
this.FooViewSource.View.Refresh();
}
然后,將您的ItemsSource
屬性綁定到ViewModel的FooViewSource
屬性。 CollectionViewSource
在其他方面也很方便。 它支持排序,過濾,選擇的項目,也許還有其他我忘記的事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.