How to do Regions in WPF without Prism? - wpf

Specifically in MVVM Light toolkit? I've not dove into the Prism code yet to see what it does in regard to regions in a WPF UI. Seems like with the messaging and the ViewModelLocator in MVVM Light you could do a similar thing....Can you? Can anyone give some examples on how you could do this? Essentially I'd like to click on a button and load two different view models into the UI. Perhaps a better way to explain is Outlook-like Navigation Pane functionality.

This can be done fairly easily in WPF, without any framework.
Just setup a DataTemplate in your Application (or at the Window/UserControl level) that maps the ViewModel to the View you wish to display for that ViewModel.
You can then just use a ContentPresenter, and bind it's contents to a single property (which can be of type object) within your ViewModel. When you want to set the "region" to a specific View, just set the property to the appropriate ViewModel, and WPF will automatically wire up everything for you.

Related

WPF MVVM Light Navigation

I'm working on a windows WPF application using MVVM Light. As far as for switching views and navigation purposes, is there set classes readily available?
Right now, I'm using a MainViewController to load other views with datatemplate and messenger class. But i'm not sure if its the best practice.
Meant for switching is ContentControl but it has nothing to do with MVVM Light. Based on its Content you can assign diffrent Views. Take a look here.

WPF DataBinding Bing Maps wpf api

I have spent countless hours reading and researching this topic – and I just can’t seem to get a foothold on it. Here is my scenario:
I write software for a company that provides asset-tracking (with some added features). We currently have an ASP.NET based website using the googlemaps api. So this is what I am comfortable with.
I have now been tasked with writing a WPF application with much of the same functionality but instead using the wpf bing maps api.
I have messed around with the map and figured out (non-mvvm way) how to draw custom pushpins, polygons, etc… Now I need to use the databinding features. This is where I just can’t seem to put the two together. Basically what I am trying to do is bind a collection of a custom class that creates a custom pushpin, to a MapControlItem.
The documentation is just a bit too fragmented and abstract for me to grab on to something – or maybe I am just too much of a web developer and really struggling to grasp a concept that is new to me.
Any ideas? Examples?
You're right, the Bing Maps WPF Control API documentation is a joke.
Anyway, you would have to use a MapItemsControl and bind its ItemsSource property to your item collection. The ItemsContainerStyle and/or ItemTemplate properties would define the UI objects that are shown on the map.
You may start reading about Data Binding to Collections.
I only played with the Windows 8 version of the Bing maps control, not the WPF one, so I apologize if my answer is not quite apropriate.
What I know, is that in windows 8, you just can't apply bindings, for MapLayers or MapChildren.
From what you describe, I believe you just can't do a binding on these properties in WPF, simply because they are not dependency properties.
So only 1 solution left, in you window's code-behind, subscribe to your ViewModel's PropertyChanged event, and manually apply any updates you need to your control.
Anoter way to do that, is to create a UserControl, which will simply display a BingMaps control, and add to this userControl a "BingMapsContext" (or whatever) dependency property, to manually update the map control when that specific property will be binded.

Difference between View and Usercontrol when doing MVVM

lately i often see questions regarding MVVM and usercontrol, where - for me - view and usercontrol are mixed up.
for me a View when doing MVVM is just a pretty interface that allows users to interact with my ViewModels - so at least a collection of controls with Bindings. most time xaml only but codebehind not forbidden.
for me a usercontrol instead is not related to a viewmodel at all. the usercontrol has a representation in xaml and of course codebehind where properties, methods and Dependency Properties exist.
i'm on a wrong way with that?
EDIT: of course view and usercontrol inherit from UserControl class - so both technically UserControls. but i use just the term View when doing MVVM. and the term usercontrol just when there is no direct relation to a viewmodel.
ps: my english is too bad to write down what i mean...
You're not wrong just consider the fact the user control can be a reusable view that has a view model. cause the entire composite ui architecture is based on s shell(main window, view) and some regions with view (user controls)
A View and User Controls are totally different in MVVM.
The View is a conceptual name of the folder where you put all UI related stuff like user controls, windows, pages, etc. So the View is the folder that contains your GUI part for the particular application.
The User Control is the control which is configured by the developer by mixing multiple components / controls to work like a single control. A user control can also hold the other user controls.
The mix point is that, generally, views hold the user control in an MVVM application, as WPF is XAML based. It gets rendered in there, so the developer can plug your, his and other's user control into some where he wants to.
Whereas windows can not be placed into other windows. And pages can only be shown in frame element, so most views are user controls.

Getting data from a view in MVVM?

I have a silverlight bing map application. I am using the MVVM pattern with PRISM.
The bing map has a "BoundingRectangle" property that is not available in XAML, but it is available via code behind. Of course this does me no good since I need the data in my viewmodel which doesn't have access to the View's code behind (nor do I want to add it, since I'd really like to try to not use the view's code behind if possible).
Normally, you would do a two way bind to a viewmodel property. The Bing map will surface BoundingRectangle for layers, but not for the base map (that I can find).
I'm not looking for a hack here, just wondering what the best practices or convention for getting data out of a view to a viewmodel that isn't "bindable".
Thanks!
Databinding in Silverlight is just a framework feature that automatically synchronizes data between your view and your view model (if you are following the MVVM pattern). However, there is nothing wrong with doing this yourself!
The two main advantages of the MVVM pattern (other than the usual separation of concerns that most UI patterns provide) are:
It aids unit testing, the View Model can be exercised from your unit test code without a view present.
It helps the developer / designer workflow, reducing the files shared between the designer and developer.
In my experience, having a small amount of code-behind that 'assists' the binding framework does no hard at all!
You can use techniques such as attached behaviours to wrap this code up, but often this just results in a cosmetic improvement.
CraigF,
you can use Mediator pattern, if you use Galasoft Light toolkit then use messenger to send message from view to your viewmodel. Viewmodel register to that message and if recive one set your property in viewmodel and do some logic..

Where should the viewModel be created?

I have seen a few examples where the viewModel (in Silverlight apps) is in the UserControl.Resources XAML section of a View. I read that for using Blend, this is a good place to have it (as it gives the ability to see sample data in Blend).
However, is this the best place to have the viewModel? I read that the "view has to push services to the viewModel". What does this mean and where else could or should the ViewModel be created?
Thanks.
JD.
There are lots of ways that the View and the ViewModel can be connected. The simplest approach is using the Resources like you mention or even easy just setting the DataContext of the View in the Xaml to an instance of the ViewModel.
From there things get more complex and really it depends on the framework you use:
Silverlight.FX - Uses a View base class with a Model property.
MVVM Light - Uses a ViewModelLocator.
Prism - Controllers
Caliburn - Presenters
So the approach you take will depend on what style you like. There are many ways to do this and right now there are a lot of MVVM frameworks showing up.

Resources