簡體   English   中英

Devexpress GridControl-在MVVM中刷新數據

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM