Devexpress GridControl - 刷新MVVM中的数据

时间:2016-03-03 15:38:50

标签: c# wpf mvvm devexpress gridcontrol

我的网格:

<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时,一切正常。但我想使用List(不要在循环中通知)。

在网格上开始滚动后,视图会刷新。有什么问题?

1 个答案:

答案 0 :(得分:0)

我认为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对其他事情也非常方便。它支持排序,过滤,选择的项目,也许还有其他一些我忘记的事情。