Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Devexpress GridControl-刷新MVVM中的数据_C#_Wpf_Mvvm_Devexpress_Gridcontrol - Fatal编程技术网

C# Devexpress GridControl-刷新MVVM中的数据

C# Devexpress GridControl-刷新MVVM中的数据,c#,wpf,mvvm,devexpress,gridcontrol,C#,Wpf,Mvvm,Devexpress,Gridcontrol,我的网格: <dxg:GridControl x:Name="StatisticsGridLevel1" dx:ThemeManager.ThemeName="Office2013" DataContext="{Binding FooViewModel}" ItemsSource="{Binding FooCollection}"> 当我使用ObservaleCollection时

我的网格:

<dxg:GridControl x:Name="StatisticsGridLevel1"
                 dx:ThemeManager.ThemeName="Office2013"
                 DataContext="{Binding FooViewModel}"
                 ItemsSource="{Binding FooCollection}">
当我使用ObservaleCollection时,一切正常。但是我想使用列表(这不是在循环中通知)


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

我认为
CollectionViewSource
在您的情况下是可行的。有很多方法可以在XAML、ViewModel和视图的代码中创建一个。为了演示的目的,我将列出最简单的一个,即在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();
}
//假设这是您的构造函数
公共视图模型()
{
this.FooViewSource.Source=this.fooCollection;
}
私有只读列表fooCollection=新列表();
私有只读集合ViewSource fooViewSource;
公共集合ViewSource FooViewSource
{
获取{返回this.fooViewSource;}
}
私人文件
{
foreach(集合中的var元素)
{
this.fooCollection.Add(new FooDto())
{
X=元素1,
Y=element.Foo2,
Z=元素0.3
});
}
this.FooViewSource.View.Refresh();
}

然后将
ItemsSource
属性绑定到ViewModel的
FooViewSource
属性。
CollectionViewSource
对于其他东西也非常方便。它支持排序、筛选、选定的项目,可能还有一些我忘记的东西。

我认为
CollectionViewSource
在您的情况下可以工作。有很多方法可以在XAML、ViewModel和视图的代码中创建一个。为了演示的目的,我将列出最简单的一个,即在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();
}
//假设这是您的构造函数
公共视图模型()
{
this.FooViewSource.Source=this.fooCollection;
}
私有只读列表fooCollection=新列表();
私有只读集合ViewSource fooViewSource;
公共集合ViewSource FooViewSource
{
获取{返回this.fooViewSource;}
}
私人文件
{
foreach(集合中的var元素)
{
this.fooCollection.Add(new FooDto())
{
X=元素1,
Y=element.Foo2,
Z=元素0.3
});
}
this.FooViewSource.View.Refresh();
}

然后将
ItemsSource
属性绑定到ViewModel的
FooViewSource
属性。
CollectionViewSource
对于其他东西也非常方便。它支持排序、筛选、选定的项目,可能还有一些我忘记的东西。

为什么不能坚持使用
ObservableCollection
?修改集合时,网格不会自动更新,除非集合实现了
INotifyCollectionChanged
ObservableCollection
实现接口
List
没有实现。因为我向集合中添加了很多项。我想在完成循环后刷新网格(手动调用Notify)。好的,我明白了,但我不太理解你的问题。这是什么意思:“在网格上启动滚轮后视图刷新”?启动滚轮是什么?你指的是鼠标滚轮吗?您正在处理鼠标滚轮事件。如果是这样的话,听起来可能与问题有关,您应该显示代码。听起来好像您在说,尽管您使用的是
列表而不是
可观察集合,但是
Foo
中的视图仍在刷新?是吗?否:)在第一步中,我使用默认值创建List FooCollection。这些值在网格中正确显示。在第二步中,我调用Update()方法。更新清理集合并添加新项目。FooCollection.Clear();FooCollection.Add(…)//元素1。。。FooCollection.Add(…)//元素50000 this.NotifyPropertyChanged(“FooCollection”);网格显示旧值。我单击网格中的滚动条(使用旧值)或单击列(排序)-网格已显示正确的数据。为什么不能坚持使用
ObservableCollection
?修改集合时,网格不会自动更新,除非集合实现了
INotifyCollectionChanged
ObservableCollection
实现接口
List
没有实现。因为我向集合中添加了很多项。我想在完成循环后刷新网格(手动调用Notify)。好的,我明白了,但我不太理解你的问题。这是什么意思:“在网格上启动滚轮后视图刷新”?启动滚轮是什么?你指的是鼠标滚轮吗?您正在处理鼠标滚轮事件。如果是这样的话,听起来可能与问题有关,您应该显示代码。听起来你好像在说景色很美
// 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();
}