English 中文(简体)
To MVVM or not to MVVM that is the question [closed]
原标题:

Closed. This question needs to be more focused. It is not currently accepting answers.


Want to improve this question? Update the question so it focuses on one problem only by editing this post.

Closed 2 years ago.

I am rewriting my windows forms based application and I am going to use WPF. The application relies heavily on drag and drop techniques, in a very graphically environment. The uses "Design" reports etc. by dragging elements onto a grid, moving them, right click setting properies etc. all of which is saved to a database. Also control program flow by drawing flow charts, with routing and desicion making, all drawn on the form, and again save to a database.

Does MVVM lend itself to this kind of application, or am I trying to fit a round peg in a square hole.

Your thoughts are appriciated.

最佳回答

My take is to use MVVM, but not religiously.

I mean, use a model to your views, but also use some code behind when needed (drag&drop, double-click). Find a balance that helps your development, without driving you nuts.

问题回答

MVVM lends itself very well to WPF. Can you do drag-drop with WPF and MVVM? Sure you can. Try searching for "WPF Drag Drop Behavior"

There are two really good reasons to go with MVVM:

  1. It helps you produce business logic and data access code that is more easily unit tested
  2. With very little extra effort, all of your UX should be easy to modify in Blend

As several posters have metioned, any eventing related to the UX can be handled in code-behind, but you should be exposing and accessing (read and write) data through your View Models for easy binding in your Views.

As for the extra effort I referred to in #2, you could easily add a static property to your App object to determine if the application is running versus a View being opened in Blend. If the View is open in Blend, leverage mock data instead of making data access calls. Here s some sample code that works for checking if Blend has a View open:

if (Application.Current == null || Application.Current.GetType() == typeof(Application))
{
    isInDesignMode = true;
}
else
{
    isInDesignMode = false;
}

Hope this helps.

If you are looking for application maintianance and testability in the long term, sure you can do that stuff with MVVM and WPF. or else just go with WPF. MMVM initial learning curve is very steep.

Patterns like MVVM is to make life simpler. So for any case if you feel like the pattern is giving you a hard time, feel free to break it or try something else. Blindly following anything will not help you. But anyways MVVM also supports complex UI interactions like Drag and Drop, I believe behaviors could help you on this. Search on google for WPF drag & drop behaviours and you could find a lot of tutorials and code to assist you on this.





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签