Force wpf control update from View Model - wpf

I currently develop a WPF control which is simply canvas with some additional properties like grid lines, children drag and drop etc.
I need a mechanism to call some of my canvas functional from view model binded to view that contains my canvas.
For example, view can contain my canvas and a button "Show grid". When user clicks the button canvas should paint grid on itself.
The problem is to do this according to MVVM pattern without direct reference to canvas in view's view model.
What is the best practice to do this?

UI related stuff can be still written in view behind cs file. It does not mean that you violated MVVM. If view models are for business logic, view behind is for view logic. To achieve more encapsulation and re-usability, you can use Behaviors.
You can write a behavior which do all painting logic and attach it to the button. Even you can add dependency properties to your behavior to have more control on it.

Related

WPF - modify window content from ViewModel

I have a WPF application. I'm trying to modify it to use the MVVM pattern.
Now, in the code behind of my main window, when I click a button, I change the "Content" of my window with a WebBrowser control :
Content = webBrowserWrapper.WebBrowser;
I would like to do it from a command in my ViewModel. How can I access the "Content" property of my window ?
If it's not possible, what is the simple way to modify the content of my window (maybe create a new window and navigate to it)
The principal behind MVVM is that UI is the concern of the View, and logic is the concern of the ViewModel.
You describe a concern of the View, which is properly handled within its codebehind. There is no need to involve the ViewModel in this action.
So, in other words... congrats. You're already there.

Custom painting in WPF view

I have a question about doing the custom painting operations in WPF MVVM View. My case is:
External manipulation of model data happens, and the observable collection of data to be shown is modified (storing some data to be shown in a diagram). I have to react to that change in my view, and custom layout the diagram elements (remove the ones not used, place new ones, calculate positions on diagram canvas). How can I do that, and what would be the best way to do it conform to MVVM pattern? I cant subclass the diagram class, as it is sealed. The diagraming framework used is MindFusion.
Edit: A solution was proposed on MindFusion Support forum, and it works.
http://mindfusion.eu/Forum/YaBB.pl?board=wpfdg_disc;action=display;num=1306412889;start=0
Last I checked MindFusion diagraming component for WPF, it supported MVVM through data binding. If you bind the diagram to a ObservableCollection you can then write custom Node Templates which are basically DataTemplate that will render YourDiagramModel items on the diagram panel.
Unlike WinForms you usually don't need to manually refresh or paint control surface in WPF custom controls.

WPF Control Grouping

I have a group of controls that look like this:
<Link to Image>
that i reuse a number of times. It's really simple a listview, 3 buttons and some layout panels.
I want to turn this into a reusable component but the columns in the listview can change and the sources they are bound to will change.
How do i go about this? i've seen many comparisons between ContentTemplates and UserControls etc but they never seem to be functional (eg Add will raise an event which i'll have to handle to add something to the listview, remove will raise an event where i'll likely ask if they are sure first).
I've accomplished the events with my own UserControl, but can't pass a list of GridViewColumns to the control. It also means i have to expose SelectedItem etc manually from the UserControl. Subclassing Listview seems promising for setup and access but doesn't conceptually seem right to have other controls in the listview area.
What is the right way?
I would definately recommend a UserControl. You should:
Add the controls you require to your user control
Add the Dependency Properties you require to your user control which allow you to configure it, e.g. SelectedItem
Wire up these dependency properties to the various controls within your user control. An easy way to do this is to set the DataContext of your user controls visual tree to the user control itself, e.g. if you have a Grid as the root for your user controls, set its DataContext = this in code. You can then use TwoWay bindings to connect up the various control properties to the user control properties.

using MVVM and WPF for a realistic visualizations

I currently need to create a visual representation of a ferry system that displays the actual ferries their position on the sea and the state of their cargo. The ferries contain trucks and the trucks contain cars. I need to display the actual trucks and their xy postion on the deck. When the ferries are loaded the postions of the trucks are updated frequently so the look animated. Also I need to display the actual cars on the trucks. Trucks, cars and ferries have some states that need to be displayed too. So I have a hierarchical structure of data that I need to visualize in a rather realistic manner.
What would be a good way to implement this kind of stuff in WPF? Should I use MVVM with one TreeView control and create a HierarchicalDataTemplates for sea, ferry, truck and car and a ControlTemplate for the TreeView? Or should I better use UserControls and compose and update them in code instead of databinding to observable collections of the ViewModel. Do you have any experience with this? How would you do this? Could you sketch out class/control setup?
I'd recommend making a "lookless" control as opposed to making user controls. Generally I use user controls as glue/container for my lookless controls. An example of a lookless control is the Button class. It contains a default style and in Blend, you can modify the style all you like. It also supports the visual state manager so you can change how the presentation looks when states change. You can think of the codebehind of a lookless control as a mini ViewModel. Here it is ok to mix some presentation stuff and your domain classes.
If you follow this same design, you could create a Ferry lookless control. This control would have a set of it's own dependency properties (possibly listening to the OnChange of the DP).
Your Ferry control may have an ObservableCollection DP called "Trucks".
Then in your Themes\generic.xaml, create a default style for your Ferry control. Your default style may have an ItemsControl with an ItemsSource={TemplateBinding Trucks}. The ItemsControl panel template, could be your own custom panel for arranging the Trucks, or maybe you use a Canvas. For the ItemsControl items template, you would have something like this:
<DataTemplate>
<mynamespace:TruckControl/>
</DataTemplate>
You Truck control, would also be a lookless control with it's own default style, and it's data context will already be set, so you can directly do the {Binding Path=xyz}. Your Truck control could also set it's Canvas.Left/Top (if you chose to use a canvas in the pervious items control..or maybe it doesn't set its position at all if you made a custom panel for it) or a render transform as to put it at the correct X,Y. You could also use the items control in the truck's template to render out the cars in the same fashion you rendered out the trucks in the ferry control. Also its possible to create states for the VisualStateManager as to make it fully Blend supportable. So if a truck goes into a "problem state" you could easily style that state in blend to make it blink red, for instance.
I know it sounds like a lot to digest, but in the end having stylable controls all supporting an MVVM model will make your life 1000000x easier.
I'd suggest studying Microsoft's silverlight toolkit to get a good idea how to do lookless controls and such. Try looking at a simple control, like the DatePicker ( http://silverlight.codeplex.com/SourceControl/changeset/view/25992# ) One caveat is ignore DatePicker.xaml file (it's just a mirror of what gets put in generic.xaml and nothing bad would happen if you just deleted it).
The things you should pay close attention to are:
1.) The attributes on the class. These help Blend know how to deal with your control.
2.) The OnApplyTemplate override. This is where you can pull out specific elements from your template. These are known as "parts" and you will see the parts tab in Blend. The attributes in #1 can define what "parts" are in the template and what type they are expected to be.
3.) The DefaultStyleKey = typeof(...) in the constructor. This tells Silverlight what default template to use in the generic.xaml
4.) Look at Themes\generic.xaml. This is a special hardcoded file location that stores all your default templates. Search for the DatePicker style and you will get the idea :)
Good luck!
I just wanted to let you know, how I actually implemented it. It turned out that it was not necessary at all, to write custom controls or UserControls for this. All I did, was writing datatemplates for the car, ship, ferry, truck etc ViewModels. For example the datatemplate for the FerryViewModel contained an ItemsControl with a ItemsPanel of type Canvas (to be able to position the trucks) and an ItemTemplate that was a DataTemplate for TruckViewModel. A very simple and fast approach.
I'd suggest having one user control handle all the drawing. Otherwise you can get lost the the hierarchy of objects. Also it makes it easier if another item was added, say people in cars, trucks and ferries.
If your model is hierarchical then you can just pass in the top level into the control, and let the control sort itself out.
MVVM works well for existing controls, but existing WPF controls only work if there's a control that's close to what you need, and with a few tweaks would work. I can't think of a standard control in WPF that's close to what you need, so it's time to write a new control.
WPF works really really well with view models. If you can keep code behind away until specifically needed then you can separate ui from data so much more easily. It will allow your ui's to be some much more upgradeable if the data model doesn't change between different display.

Hook in datatemplate view switch/change

I am switching my UserControls via DataTemplate. When I leave the UserControl/DataTemplate View I need to ask wether the user wants to save or not because else all data will be lost like graphical location points x,y of a user drag/dropped Rectangle on a canvas.
How can I hook into the datatemplate change and bring up my own save dialog?
So you're binding a ContentControl's Content property to a property in your viewmodel?
The best place to implement this workflow would be where you normally set this viewmodel's property. Doing this in the viewmodel, rather than trying to hook into events in the View layer means you have more control and testability.

Resources