[英]Devexpress GridControl - refresh data in MVVM
My grid: 我的网格:
<dxg:GridControl x:Name="StatisticsGridLevel1"
dx:ThemeManager.ThemeName="Office2013"
DataContext="{Binding FooViewModel}"
ItemsSource="{Binding FooCollection}">
ViewModel: 视图模型:
private List<FooDto> fooCollection = new List<FooDto>();
public List<FooDto> FooCollection
{
get
{
return this.fooCollection;
}
private set
{
this.fooCollection = value;
this.NotifyPropertyChanged();
}
}
And example method: 和示例方法:
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");
}
When I use ObservableCollection, everything works fine. 当我使用ObservableCollection时,一切正常。 But I want to use the List (that's not to notify in the loop).
但是我想使用列表(不是在循环中通知)。
The view refreshes after the start scroll on the grid. 在网格上开始滚动后,视图将刷新。 What is the problem?
问题是什么?
I think a CollectionViewSource
would work in your case. 我认为
CollectionViewSource
可以在您的情况下工作。 There are a lot of ways to go about creating one, in XAML, in your ViewModel, in your View's code-behind. 有很多方法可以在XAML中,在ViewModel中以及在View的代码隐藏中创建一个。 I will throw together the easiest one for demonstration purposes which is creating a
CollectionViewSource
property on your ViewModel. 我将把最简单的一个放在一起进行演示,这是在ViewModel上创建
CollectionViewSource
属性。 I think some people might not necessarily like this approach - it kind of has the feel of mixing concerns. 我认为有些人可能不一定喜欢这种方法-有点让人担忧。 I am not sure I agree, though.
不过,我不确定我是否同意。 If you take the position that a
CollectionViewSource
is an object model for a collection's view then I don't see anything wrong with having it in your ViewModel. 如果您认为
CollectionViewSource
是集合视图的对象模型,那么我认为ViewModel中没有任何问题。 But I think because it inherits from DependencyObject
it gets stigmatized as being more of a view concern. 但我认为,由于它继承自
DependencyObject
因此受到了更多的关注,因此受到了耻辱。 Anyway, something like this would do what you want: 无论如何,这样的事情会做你想要的:
// 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();
}
Then you would bind your ItemsSource
property to the FooViewSource
property of your ViewModel. 然后,将您的
ItemsSource
属性绑定到ViewModel的FooViewSource
属性。 A CollectionViewSource
is pretty handy for other things as well. CollectionViewSource
在其他方面也很方便。 It supports sorting, filtering, selected items, maybe some other things I am forgetting. 它支持排序,过滤,选择的项目,也许还有其他我忘记的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.