WPF App Design Thoughts
-
I'm sort of there on this... Can you show me how in the MainWindowViewModel this code is used? For example, if the user selects Show All Customers, how does the template above get used/loaded in the MainWindowViewModel? I saw in his article where it says "The MainWindowResources.xaml file has a ResourceDictionary. That dictionary is added to the main window's resource hierarchy, which means that the resources it contains are in the window's resource scope. When a tab item's content is set to a ViewModel object, a typed DataTemplate from this dictionary supplies a view (that is, a user control) to render it" So the Content is set to a VM, not a View?
If it's not broken, fix it until it is
That's correct. The appropriate
DataTemplate
is picked because it matches the ViewModel. In other words, it looks at theDataType
to pick up the appropriateDataTemplate
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
That's correct. The appropriate
DataTemplate
is picked because it matches the ViewModel. In other words, it looks at theDataType
to pick up the appropriateDataTemplate
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
Ah, right, forgot about just setting the DataTemplates based on VM type... Much cleaner than a DataTemplateSelector... So, what Pete said :)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Ah, right, forgot about just setting the DataTemplates based on VM type... Much cleaner than a DataTemplateSelector... So, what Pete said :)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Hey, you and me are in this WPF thing together bro. ;)
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
-
Hey, you and me are in this WPF thing together bro. ;)
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
In it? Man, I'm up to my neck in it, and it's great :) Granted, I tend to stray from proper MVVM now and then, but I gotta say, WPF enables some nice-looking and stable interfaces... Never going back to WinForms :)
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
That's correct. The appropriate
DataTemplate
is picked because it matches the ViewModel. In other words, it looks at theDataType
to pick up the appropriateDataTemplate
.*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier
So I'm assuming that the VM is bound to the view automatically?
If it's not broken, fix it until it is
-
So I'm assuming that the VM is bound to the view automatically?
If it's not broken, fix it until it is
Ideally, you don't want to change the DataContext property from code. The usual method is to set the entire window to a ViewModel, and each section can have its DataContext bound to a property on that ViewModel. MainWindow --> DataContext = MainWindowVIewModel ContentFrame --> DataContext bound to ContentModel property (on MainWindowViewModel) HelpFrame --> DataContext bound to HelpModel property (on MainWindowViewModel) That way, when you change the content, all you do is change, in this example, the ContentModel property. Since the GUI control is hooked up with databinding, it'll automatically grab the new data, and switch to the proper DataTemplate. So in short, you want to set the uppermost DataContext property once on window load, then operate exclusively through ViewModel properties.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Ideally, you don't want to change the DataContext property from code. The usual method is to set the entire window to a ViewModel, and each section can have its DataContext bound to a property on that ViewModel. MainWindow --> DataContext = MainWindowVIewModel ContentFrame --> DataContext bound to ContentModel property (on MainWindowViewModel) HelpFrame --> DataContext bound to HelpModel property (on MainWindowViewModel) That way, when you change the content, all you do is change, in this example, the ContentModel property. Since the GUI control is hooked up with databinding, it'll automatically grab the new data, and switch to the proper DataTemplate. So in short, you want to set the uppermost DataContext property once on window load, then operate exclusively through ViewModel properties.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)I understand that, but Pete pointed me to this[^], and I'm wondering where or how in this design is the data context set? So, if the content area is defined like this
In the MainWindowViewModel I would then set the MainContent property to the MainContentViewModel. How does the MainContentViewModel.DataContext get set. It won't know about the MainWindowViewModel. Unless I'm missing something?
If it's not broken, fix it until it is
-
I understand that, but Pete pointed me to this[^], and I'm wondering where or how in this design is the data context set? So, if the content area is defined like this
In the MainWindowViewModel I would then set the MainContent property to the MainContentViewModel. How does the MainContentViewModel.DataContext get set. It won't know about the MainWindowViewModel. Unless I'm missing something?
If it's not broken, fix it until it is
DataContext is inherited, so when the window itself is created, set its DataContext to a root ViewModel class. Then when you use the code you just posted, it would bind to the "MainContent" property of that ViewModel, which would be your MainContentViewModel (Or whatever you decide to call it). The initial DataContext property for the window is set in code... After that, everything is data-bound.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
DataContext is inherited, so when the window itself is created, set its DataContext to a root ViewModel class. Then when you use the code you just posted, it would bind to the "MainContent" property of that ViewModel, which would be your MainContentViewModel (Or whatever you decide to call it). The initial DataContext property for the window is set in code... After that, everything is data-bound.
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels)Got it! Thanks
If it's not broken, fix it until it is
-
Got it! Thanks
If it's not broken, fix it until it is
No problem, man. Glad we could help
Proud to have finally moved to the A-Ark. Which one are you in?
Author of the Guardians Saga (Sci-Fi/Fantasy novels) -
Got it! Thanks
If it's not broken, fix it until it is
I insist you to look on my article, Building High Performance WPF/E Enterprise Class Application in C# 4.5[^] This will have greater help in understand what are all different layers, what need to considered for better performance, low memory oriented applications