Click here to Skip to main content
15,890,527 members
Home / Discussions / WPF
   

WPF

 
QuestionProper way to do this in MVVM? Pin
SledgeHammer0126-Aug-13 11:46
SledgeHammer0126-Aug-13 11:46 
AnswerRe: Proper way to do this in MVVM? Pin
Pete O'Hanlon26-Aug-13 12:39
mvePete O'Hanlon26-Aug-13 12:39 
GeneralRe: Proper way to do this in MVVM? Pin
SledgeHammer0126-Aug-13 12:56
SledgeHammer0126-Aug-13 12:56 
AnswerRe: Proper way to do this in MVVM? Pin
Abhinav S26-Aug-13 16:29
Abhinav S26-Aug-13 16:29 
QuestionTreeView Pin
columbos1492726-Aug-13 2:32
columbos1492726-Aug-13 2:32 
AnswerRe: TreeView Pin
Jason Gleim27-Aug-13 5:36
professionalJason Gleim27-Aug-13 5:36 
GeneralRe: TreeView Pin
columbos1492727-Aug-13 7:46
columbos1492727-Aug-13 7:46 
GeneralRe: TreeView Pin
Jason Gleim27-Aug-13 8:11
professionalJason Gleim27-Aug-13 8:11 
Not really... Let's say you have three user controls; myControlA, myControlB, and myControlC. You have a MainPage with a tree view and a content control. The selection change on the tree view is passed back to the main view model via an ICommand just the same as a button or other view event.

The content control is bound to a content property on the view model which is implemented as a dependency property (required to support the bindings).

When a node is clicked on the tree view, the ICommand fires a method on the ViewModel. That method, in turn, creates an instance of myControlA and assigns it to the content property:
C#
myContentProperty = new myControlA;

Because the content control's content property is bound to your content dependency property on the VM, it detects the change and updates the content presenter area with the new instance of myControlA. In your method to determine what gets loaded, you simply decide if you are creating an instance of myControlA, myControlB, myControlC, etc.

This doesn't break MVVM because we aren't creating a dependency on the user controls. You could, in fact, create a content object and assign the template xaml all in code then assign it to the content DP making it show on your form. In that case, the view doesn't even exist until runtime! If you took the view model and fired it up in a test harness, you would execute the ICommand and test to see that the content property gets set to the proper user control type. The fact you can do that demonstrates we didn't break MVVM.

The separation of concerns is to enable encapsulation and testing. Your view in this case is your main page and the VM is your main VM. You can't have a reference to the main view in your view model... that is a no-no... But creating a sub-view (which is itself encapsulated) is encouraged. In fact, if you look at the way App.xaml is setup, it does the same thing. Your app is actually rooted at app.xaml and mainpage.xaml is a sub-view of app.xaml.

The important thing, however, is to make sure that your sub views (the user controls) DO NOT have any bindings to the main view model. They should each bind to their own view models. While it isn't strictly against the rules to bind the sub view to the main view model, it makes it easier to keep things straight with the data contexts.

HTH!
GeneralRe: TreeView Pin
columbos1492727-Aug-13 19:29
columbos1492727-Aug-13 19:29 
QuestionCurrency Converter(Silverlight+MVVM) Pin
Ipsita_2324-Aug-13 1:48
Ipsita_2324-Aug-13 1:48 
AnswerRe: Currency Converter(Silverlight+MVVM) Pin
Abhinav S24-Aug-13 2:10
Abhinav S24-Aug-13 2:10 
GeneralRe: Currency Converter(Silverlight+MVVM) Pin
Ipsita_2324-Aug-13 6:29
Ipsita_2324-Aug-13 6:29 
GeneralRe: Currency Converter(Silverlight+MVVM) Pin
Richard MacCutchan24-Aug-13 6:36
mveRichard MacCutchan24-Aug-13 6:36 
AnswerRe: Currency Converter(Silverlight+MVVM) Pin
Abhinav S24-Aug-13 17:37
Abhinav S24-Aug-13 17:37 
GeneralRe: Currency Converter(Silverlight+MVVM) Pin
Ipsita_2325-Aug-13 1:09
Ipsita_2325-Aug-13 1:09 
QuestionControl's Border Color animation Pin
thecco23-Aug-13 7:08
thecco23-Aug-13 7:08 
AnswerRe: Control's Border Color animation Pin
koll Zhu22-Sep-13 23:49
koll Zhu22-Sep-13 23:49 
Questionhow to show data in combobox from database in silverlight Pin
Member 793334323-Aug-13 1:22
Member 793334323-Aug-13 1:22 
AnswerRe: how to show data in combobox from database in silverlight Pin
Richard MacCutchan23-Aug-13 2:56
mveRichard MacCutchan23-Aug-13 2:56 
AnswerRe: how to show data in combobox from database in silverlight Pin
Brian Holsen25-Aug-13 18:22
Brian Holsen25-Aug-13 18:22 
AnswerRe: how to show data in combobox from database in silverlight Pin
koll Zhu22-Sep-13 23:49
koll Zhu22-Sep-13 23:49 
QuestionWPF DataGrid ComboBox Column Binding Problem Pin
Kevin Marois22-Aug-13 5:13
professionalKevin Marois22-Aug-13 5:13 
QuestionWPF - Understanding Binding Syntax Pin
Kevin Marois20-Aug-13 8:29
professionalKevin Marois20-Aug-13 8:29 
AnswerRe: WPF - Understanding Binding Syntax Pin
Mycroft Holmes20-Aug-13 12:52
professionalMycroft Holmes20-Aug-13 12:52 
GeneralRe: WPF - Understanding Binding Syntax Pin
Kevin Marois20-Aug-13 15:05
professionalKevin Marois20-Aug-13 15:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.