WPF ViewModel 不是活动演示者
有一个 ViewModel 由一些相关对象(节点和线( , 如何在视图中显示(同步)这些虚拟机并保持对象连接。 我使用一些 DataTemplate 将模型映射到视图,但每个对象都会同步(通过强大的绑定)到其相关对象,但我如何将此 DataTemplate 生成的 UI 元素链接(并同步)在一起。 我在这里从另一个角度描述问题: 同步视图模型和视图
There is a ViewModel that consists of some related object (nodes and lines( ,
How it can be possible to display (synchronize) these VM in View and keep object connections.
I use some DataTemplate to map model to view but each object would be synchronized (with powerful binding) to its related object but how can i link (and synchronize) this DataTemplate generated UI element together.
I describe problem from another viewpoint here:
Sunchronizing view model and view
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了保持视图同步,您应该使用绑定,但您的绑定源需要实现某些接口或依赖属性。对于集合,您需要实现
INotifyCollectionChanged
以及您要使用的属性INotifyPropertyChanged
,如果您随后更改源,您的视图也会更改。To keep your view synchronized you should use bindings, your binding sources need to implement certain interfaces or be dependency properties though. For collections you need to implement
INotifyCollectionChanged
and for properties you'd useINotifyPropertyChanged
, if you then change the source your view will change as well.除了实现 INotifyPropertyChanged 并使用 ICollectionChanged (ObservableCollection) 并绑定到视图之外,您可以考虑实现 IEditableObject 当你想支持数据编辑时。
该界面允许您撤消编辑操作。如果不实现 IEditableObject,您需要在取消修改时返回数据源将 重置为原始值。 DataGrid 也支持该接口。
Next to implementing INotifyPropertyChanged and using ICollectionChanged (ObservableCollection) and binding to the views, you might consider implementing IEditableObject when you want to support the editing of the data.
This interface allows you to undo edit actions. Without implementing IEditableObject you would need to go back to the data source to reset the to the original values when canceling the modifications. The interface is also supported by the DataGrid.
您可以使用中继命令逻辑同步 VM 和视图。
您可以在此处查看示例锻炼
使用MVVM绑定WPF中窗口的“WindowState”属性
You can synchronize the VM and View using Relaying Command Logic.
you can see a sample workout here
Binding the "WindowState" property of a window in WPF using MVVM