使用MVVM在WPF中的画布控件上绘图

时间:2021-12-09 16:53:21

I'm trying to convert my application to use the MVVM pattern. I currently have a custom canvas object that inherits form the standard canvas object, and overrides the OnRender event.

我正在尝试将我的应用程序转换为使用MVVM模式。我目前有一个自定义画布对象,它继承了标准画布对象,并覆盖了OnRender事件。

I am not quite sure how to convert this method to use the MVVM pattern.

我不太确定如何转换此方法以使用MVVM模式。

Currently, I have an event wired up to different keypresses, to control the canvas and allows the user to scroll. I have gotten the event stuff wired up in MVVM using MvvmLight. I am just unsure about how when the Command fires in the view, that it updates the canvas. Currently there is a method that is called, that tells the control which direction the user has moved. (I basically pass in a location which is the index of an array).

目前,我有一个事件连接到不同的按键,以控制画布并允许用户滚动。我已经使用MvvmLight在MVVM中连接了事件。我只是不确定Command在视图中触发的时间,它会更新画布。目前有一个被调用的方法,它告诉控件用户移动了哪个方向。 (我基本上传入一个位置,它是一个数组的索引)。

Just not sure what I should be doing here.

只是不确定我应该在这做什么。

2 个解决方案

#1


0  

I ended up going with DependencyProperties and using the DependencyProperties OnPropertyChanged Event Callback to update the graphics.

我最终使用DependencyProperties并使用DependencyProperties OnPropertyChanged事件回调来更新图形。

#2


-1  

In our team we also have recently converted silverlight project to mvvm pattern. I don't know is it really required for this particular view to have a view model code to do canvas navigation stuff. If it is not I'd rather recommend to leave this code in view. Otherwise you can expose the

在我们的团队中,我们最近还将silverlight项目转换为mvvm模式。我不知道这个特定视图真的需要一个视图模型代码来做画布导航的东西。如果不是,我宁愿建议将此代码保留在视图中。否则你可以揭露

public Point CurrentCoordinate 

property and bind to it from view.

属性并从视图绑定到它。

#1


0  

I ended up going with DependencyProperties and using the DependencyProperties OnPropertyChanged Event Callback to update the graphics.

我最终使用DependencyProperties并使用DependencyProperties OnPropertyChanged事件回调来更新图形。

#2


-1  

In our team we also have recently converted silverlight project to mvvm pattern. I don't know is it really required for this particular view to have a view model code to do canvas navigation stuff. If it is not I'd rather recommend to leave this code in view. Otherwise you can expose the

在我们的团队中,我们最近还将silverlight项目转换为mvvm模式。我不知道这个特定视图真的需要一个视图模型代码来做画布导航的东西。如果不是,我宁愿建议将此代码保留在视图中。否则你可以揭露

public Point CurrentCoordinate 

property and bind to it from view.

属性并从视图绑定到它。