我们可以在WPF应用程序中的viewmodel中使用pointcollection吗? [英] Can we use pointcollection in viewmodel in WPF aaplication?

查看:102
本文介绍了我们可以在WPF应用程序中的viewmodel中使用pointcollection吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在视图中为画布上的不同对象绘制图形。我可以使用PointCollection存储不同对象的点。并创建一个PointCollection类型的属性。然后在xaml中我将该行绑定到此PointCollection属性。



我尝试过:



我只需要知道它是否可能。

I want to plot graph for different objects on canvas in the view. Can I use PointCollection for storing the points for different objects. And make a property of type PointCollection. Then in xaml I will bind the line to this PointCollection property.

What I have tried:

I just need to know is it possible or not.

推荐答案

最好检查的地方是文档: PointCollection类(System.Windows.Media)| Microsoft Docs [ ^ ]



如果您正在使用MVVM设计模式,那么您正在查看使用数据绑定(WPF) Microsoft Docs [ ^ ]。要使数据绑定与XAML一起使用,您需要挂钩数据绑定通知系统。查看上面链接中的 PointCollection 代码,它不会执行此操作:

Best place to check is in the documentation: PointCollection Class (System.Windows.Media) | Microsoft Docs[^]

If you're looking a using the MVVM Design Pattern, then you're looking at using Data Binding (WPF) | Microsoft Docs[^] . For Data Binding to work with XAML, you need to hook into the data binding notification syste. Looking at the PointCollection code from the link above, it does not do this:
[System.ComponentModel.TypeConverter(typeof(System.Windows.Media.PointCollectionConverter))]
public sealed class PointCollection : System.Windows.Freezable, IFormattable, System.Collections.Generic.ICollection<System.Windows.Point>, System.Collections.Generic.IEnumerable<System.Windows.Point>, System.Collections.Generic.IList<System.Windows.Point>, System.Collections.IList



您需要做的是为 PointCollection 类编写一个Proxy类包装器,该类实现 System.Collections.Specialized.INotifyCollectionChanged System.ComponentModel.INotifyPropertyChanged 数据绑定接口正常工作。



要查看前其中,请看一下数据绑定集合的ObservableCollection< T>类(System.Collections.ObjectModel) [ ^ ]。


What you need to do is write a Proxy class wrapper for the PointCollection class that implements both the System.Collections.Specialized.INotifyCollectionChangedand System.ComponentModel.INotifyPropertyChanged interfaces for Data Binding to work properly.

To see an example of this, take a look at the data binding collection ObservableCollection<T> Class (System.Collections.ObjectModel)[^].


这篇关于我们可以在WPF应用程序中的viewmodel中使用pointcollection吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆