Hey guys, wanted to clarify something: Many videos I make are designed around a general approach. Rather than dive into the nitty-gritty, my goal is to give you the basic gestalt of the concept. Normally I do NOT: 1) Use events (See my command videos) 2) Instantiate new view models every button click (See my notepad/contact book series) I usually have separate videos for the concepts that I do have to cut corners on since some concepts have prerequisite knowledge of others. My content is intended to guide people in a better general direction, and should not be interpreted as "The Answer" based on 1 video (As this would be relatively hard to compress into a 20 minute video). If you want to see all the concepts tied together for better understanding you may want to check out some of my video series, such as the ContactBook or Notepad series. Thanks!
I'm older and have plenty of C skills from decades ago but have been trying to learn C#, WPF, XAML. I've been designing an application for my wife that talks to a DB using C# to learn by as that is the only way I learn, by building a real app. I became stuck trying to more-less figure out how to do exactly what you covered here in 20 mins. Thanks a million. Spot on. You were first page for my Google search, so good SEO for you.
I too had been searching google and watching videos trying to piece this together. This is perfect. I am new to WPF but have over 25 years experience in software development. Thank you.
I have found your videos quite helpful, where others try to teach me everything at one time yours seem to focus on one aspect at a time. This makes it easier for me while trying to use it in an application I am working on. Thanks I hope you do more I think I understand how each view is accessed I would like to know more about how to move all my business rules into the Models folder.
Thank you, James. I try to make my content "modular" in a way so that a viewer can easily configure and watch content according to their needs. This way they do not get distracted by things they are not ready for, nor get distracted by things they already understand. I definitely plan on doing many more, but it is getting near the end of the semester for school, so I have been quite busy lately. Always feel free to email me suggestions or questions at: toskerscorner@outlook.com
These videos are spot on. The right amount of content, extremely well delivered. I've looked at many online tutorials - these are the best I have discovered.
I like this extreme coding style of learning. It goes deep and your explanations were great! good job. Although thats not what I came here for but I learned something I did not know so PLUS Experience.
Great Video! Not exactly what I needed but some good information. I will be looking for more of your tutorials as you seem to do a great job of explaining things.
It is a really helpful tutorial. Your short and focused explanation is great to understand. Edit: I added another view and wanted it to be the default view, but it doesn't work. Could you explain how to set a view as default?
Thanx for the great sample, I am looking to extend this, the question and task I have is to create multiple instances with code behind of the same view, and use the Obj instance to work dynamically with the instances (eg like the tabs in VS, all have same window, but the instances will save to different files, show different code and behave different, but using one and the same XAML page with code)
Oh, this is a nice video tutorial. Thank you very much. This is exactly what I was looking for. Just out of curiosity, do you think this would work with anything other than a user control? Say, like a Page or something like that?
Hello ToskersCorner, I love your tutorial, but unfortunately I am very bad in English and especially the audio, so I may be missing an information so me question. Is there a need to close the old views? For example when you switch from red to blue, red is always loaded? where it has been automatically closed. I ask you this because, my views contain information that it will look for on a system (PLC) by an RS232 communication. Each view will look for different information. If a page that has been opened remains open when you change your view, it will continue to search for data on the RS232 and overload the communication. I hope you understand what I'm trying to say to you, sorry for my english. Thanks for your feedback.
very helpful thank you!! I have a question about this, if i have a button in my red View and i want to change from there so the green view, is there a way to do it? or can i remove the data context from the red view ? Thanks a lot for your nice tutorial!
Thank you so much lol. I've been trying to achieve this but couldn't figure out what it was called. I was trying with tabcontrol. Very much appreciated now I'll fix all my coding
Interesting. Why did you decide to split the grid up into a bunch of small cells? This sort of reminds me of Bootstrap's approach to responsive mobile UI development. Is that the goal?
is it possible to change the view with a butten from ur current view(e.g from blue to red with a button in the red view ) without using the buttons on the menu bar?
Glad you asked. Until I am able to do a video on it, I made a post on my website to address your question. I also provided an example project on GitHub to show you how this is done. Link is in the description. :)
The "BaseViewModel" is a base for all viewmodels EXCEPT the main. we bind our current view to. So when we bind our view to the "ViewModel" property in our MainViewModel, we can change the property(To a Red/Blue View Model) to change the view. Since Red/BlueViewModels are derived from BaseViewModel we are able to assign them to that property.
Great video, do you happen to have any videos showing how to nest this functionality? For example, having the new view model display a set of buttons and then when those buttons are clicked, another view replaces that view. Edit: I think I found what i needed. Sounds like NavigationService will better serve my purpose
I really appreciate your helping, sharing your skills. I wonder do you make a number of viewmodels classes instead 1 class? they seem like work all the same. I tried with one viewmodel class and it does work. Also, I want to know how to set the default page without clicking any buttons.
I made multiple just to show how you theoretically would do this with different views/view models. Ideally in a real word scenario each view would be doing something different thus needing different view models.
You use it pretty much any time you wish the user to navigate to different content. I good example could be my C#.Net Sales Monitoring Tool I have in my videos. Or even my Memory Game which has a little main menu. For another example: In WinForms you normally would open a new window to accomplish these things. With WPF you can make the content of a window dynamic instead of creating new windows every time. Sort of like page navigation in your browser.
Hey, Gerry. Sorry I didn't respond. I must have missed your comment! For more specific questions I ask that you email them to me at ToskersCorner@outlook.com ! I always check my email and respond as soon as possible. Thanks!
Awesome tutorial, but I do have a question, how is this performance wise? Because every time you hit the button a new instance of the usercontrol is created right? or I'm wrong here?
Rolo B. (1/2)Great question! Yes, this is creating a new instance each time, i suppose i should have emphasized this is a demonstration. If you had an application where doing this would cause worrisome performance penalties we would simply place the instances in a wrapper class and alternate the values without recreating them everytime. We also could store them in our XAML resources if we wanted to prevent codebehind as well. This tutorial was simply showing the concept in an abstract, because I assume most people would need to adjust it to their needs anyways.
Rolo B. (2/2)However i want mention specifically that we are not creating new user controls, but rather new instances of their data contexts. So the problem lays upon that, not the user controls.
Hello, sorry for the late response. The benefits really are that user controls are designed to be, well, controls. They are designed for desktop applications, and allow you to create your own "control" view vs. opening another window. Pages, are primarily intended for a web application or browser application. So, in the end it is not much about "benefit" but more so based on what each is intentionally designed for.
Generally no. It would be best to keep your ViewModel instance stored somewhere for future access if you plan and re-using it. Perhaps in a single instance, or a collection list of view models in a main-detail view.
Hi Tosker, Is it possible to have two or multiple content control elements for one window? If possible, how DataContext shared for binding? Thanks in Advance Vino
I hate giving the answer, but it is true in almost anything programming related: It depends. If, for example, you want two navigation menus (Maybe a main nav, and side bar) you would want a 'MainViewModel' holding two different data context sets, and have them bind and call back to the parent data context.
You can add DataContext = new VM() in the constructor of the code behind, where VM() is the ViewModel in which you want to have as default. This causes that view to be opened when main window is loaded
Great video! Your tutorials are helping me doing my university projects :D Can you please explain the view models need? That parte isnt clear on my mind
The "view model" is not necessarily "needed". This is just how you would go about it conducting an MVVM type pattern. However, the reason we need a class object(View model) is because the DataTemplate we display is based on the Type of it's DataContext (Which would be the view model) (See 9:45). We tell it, if the DataContext of our window is BlueViewModel, then we want it to display the BlueView. The only way it can know what view to display, is if it can determine what type of DataContext is current(BlueViewModel). Hope I cleared it up a bit!
Great Video! Question - I'm new to WPF, but I wonder why do you need to create a ViewModel to translate between 2 different views? It would be much simpler to if the Click button event would create a new *view* and not a *viewmodel*, and then you wouldn't need the data templates (i.e. DataContext = new RedView(), instead of new RedViewModel() ). So are there any benefits for this? I understand you want ViewModels as intermediates between Views (=UI) and Models (=Business logic) - but shouldn't you let the views talk between themselves?
In my opinion, you should not let the "views" talk between themselves. However, this depends on what you what you consider "talking". What using data templates provide is reusability, flexibility, and extensibility. Also, models are not necessarily business logic, that is the job of your ViewModel. Models are simply, well.. models. They are essentially objects for structuring sets of information -- not manipulating them. However, to give you a better view(No pun intended) of my perspective: Let's say the following is a directory of views: Red View -> Blue View Blue View -> Yellow View Yellow View -> Red View Orange View -> Blue View Notice how we display a view multiple times in different ways? In your proposal we would have to hookup the code-behind of Red and Orange to do the same thing. Whereas if we already have a data template in our resources, we simply set its data context in the XAML and views change accordingly. I will admit, it is understandable you would feel this way, since the view is using code-behind to set the data contexts in this video. However, we should remember the DataContext can be set through binding, or directly in our XAML. Your suggestion will work. There is nothing "wrong" about it. It is just that by using patterns appropriately, if makes development much easier when changes and updates are inevitably made. As you make applications in WPF, some of these suggestions and methods that seem overkill will start to appear very useful. It's an understanding that is usually better understood through experience rather than books and videos. Many times I have felt using some of these patterns was pointless, to then soon run into a problem it may have fixed. Also, I would lastly add that there is sometimes a difference in doing what is "easy" and doing what is "simple". Sometimes things are only easy in the short term and not the long term, and it's up to you to evaluate the difference. Good question, I hope I didn't make anything more confusing! As I try to remind people in some of my videos, I am not an 'authority' figure on these subjects. Just a normal guy with passion for programming. People should always use their own minds to filter and research things they learn -- from me and from anyone else.
As much as I want to learn WPF, I feel like its a losing battle trying to learn it. It is just so complex and I can't seem to find and resources that teach you from the ground up. Watching random videos here and there and reading forum posts just aren't cutting it.
is there any way to preserve the data that exist in one view? because when i click to change the view and come back to the same view i get the "Default" view
Thank you! I'm unsure how much more I can really cover specifically with WPF that I haven't already. I am considering doing a small series on C# in general, to perhaps attract more 'beginners' to the channel. Though, I assume most of my subscriber base likely doesn't need such a series. I figured starting out with an easy series will help get me into the swing of things again once I find the time. If there is anything you are interested in that I have not already covered, please let me know.
How do you hand over data from the child VM to the parent? This is what I am wondering. There are cases in which you want to update a few options inside a dialog which should affect the data in your main window.
what if I enter some data in RedView Datacontext and then go to Blue View and come back to Red View. Will it reset the DataContext? everytime you click on button.
Hey Tosker I have a question what would be the best way to handle Dynamic list of objects in a treeview in an mvvm way... For example say if I have an oject class and i want to everytime i expand an object node i hit the database to get another list of objects based on a parent key field and if i expand any of these objects i hit the database and do the same thing again..
Well, I'm not so sure you would want to be hitting the DB every time a tree view is having its children indexed. Unless it is particularly sensitive information, or too massive for memory to temporarily store, I would consider loading them all into an object using WPF's ObservableCollection for storage. Perhaps an Model that forms a sort of 2D array. Hard to really unpack an answer with a lot of considerations to be made.
Rio The Hitman Unfortunately this question is too vague for a direct answer. you can email me at toskerscorner@outlook.com with more specifics and your project code so i can have a better look.
First, sorry this is about a 6 month delay in response -- must of missed this comment. Second, I do tutorials generally at requests of viewers. You can always email me for suggestions!
I found a similar way to do this and was wondering if there are pros or cons to this approach? I am new to wpf / xaml so am not sure. loginWindow.Visibility = Visibility.Visible; toolWindow1.Visibility = Visibility.Collapsed; toolWindow2.Visibility = Visibility.Collapsed;
Hello, I'am still a noob. Is it ok if we bind differents resource type into differents views(UC). For exemple the red view manage customers, the blue view manage products,... And is it possible to define data-context into redViewModel because I don't want to put all the viewchanger(navigation) into de MainWindow.cs with RedClicked, BlueClicked,... Sorry for my english, thank you for your video !
Typically you would have a view and a view model, and break up these responsibilities as you see fit. You can also have othere DataContexts in other view models, depending upon how you are designing. Try checking out some of my series videos, such as the "Contact Book Series" if you want a better idea of updating and changing view content.
Hey guys, wanted to clarify something:
Many videos I make are designed around a general approach. Rather than dive into the nitty-gritty, my goal is to give you the basic gestalt of the concept.
Normally I do NOT:
1) Use events (See my command videos)
2) Instantiate new view models every button click (See my notepad/contact book series)
I usually have separate videos for the concepts that I do have to cut corners on since some concepts have prerequisite knowledge of others. My content is intended to guide people in a better general direction, and should not be interpreted as "The Answer" based on 1 video (As this would be relatively hard to compress into a 20 minute video). If you want to see all the concepts tied together for better understanding you may want to check out some of my video series, such as the ContactBook or Notepad series.
Thanks!
Thank you. I've been learning wpf with you.
I think your general approach to these design tutorials is the best for learning. Thank you so much for taking the time to make them.
Any new programmer who thinks its best to learn everything at once.... is smarter than me. Thank you for breaking it down.
I learned more from this video than hours of trawling through Google. Straight to the point. Great content.
I'm older and have plenty of C skills from decades ago but have been trying to learn C#, WPF, XAML. I've been designing an application for my wife that talks to a DB using C# to learn by as that is the only way I learn, by building a real app. I became stuck trying to more-less figure out how to do exactly what you covered here in 20 mins. Thanks a million. Spot on. You were first page for my Google search, so good SEO for you.
I have searched the entire youtube for this tutorial, finally someone who has done it the i expected
I too had been searching google and watching videos trying to piece this together. This is perfect. I am new to WPF but have over 25 years experience in software development. Thank you.
Oh my god, just thank you. You did more for me in 19 minutes than lecturer in my university in 2 classes
This saved me yesterday from breaking my keyboard because my university course didn't explain MVVM enough. Thank you.
I have found your videos quite helpful, where others try to teach me everything at one time yours seem to focus on one aspect at a time. This makes it easier for me while trying to use it in an application I am working on. Thanks I hope you do more I think I understand how each view is accessed I would like to know more about how to move all my business rules into the Models folder.
Thank you, James. I try to make my content "modular" in a way so that a viewer can easily configure and watch content according to their needs. This way they do not get distracted by things they are not ready for, nor get distracted by things they already understand. I definitely plan on doing many more, but it is getting near the end of the semester for school, so I have been quite busy lately. Always feel free to email me suggestions or questions at: toskerscorner@outlook.com
This was probably the best tutorial I have ever seen.
Thank you!
Great tutorials. I can take a lot more from this than reading articles that often just skip setup etc.
These videos are spot on. The right amount of content, extremely well delivered. I've looked at many online tutorials - these are the best I have discovered.
The best tutorial for MVVM
Did a great job on the video. It's still very applicable today. Thank you for taking the time.
Excellent stuff! Thank you Toskers!👍
I like this extreme coding style of learning. It goes deep and your explanations were great! good job. Although thats not what I came here for but I learned something I did not know so PLUS Experience.
Excellent vid. Just clicked on your channel and found nirvana! Looks like I'm going to be busy for a while watching your vids. Thx!
Your videos are really good… shouldn’t put yourself down…. I’m finding them very helpful
You are a genius, finally an actual useful tutorial
Thank you Sir! Much appreciated. Struggled a lot with this topic. Very clearly explained.
Good tutorial for me, I'm starting to learn WPF+ MVVM and this help me a lot ! Thanks you
Thanks, man! Hope my content continues to help!
Excellent tutorial. Good job!
One of the best done videos I've ever seen! Thank you!
Dude this was super helpful, and super simple to follow. Exactly what I was trying to find haha. Thanks!
Extremely useful tutorial. Thank you man.
Thanks, this saves my ass at the deadline
Great Video! Not exactly what I needed but some good information. I will be looking for more of your tutorials as you seem to do a great job of explaining things.
Many thanks, man. Very good explanations. Good job
Just what I needed. Thanks. Keep up the good work.
It is a really helpful tutorial. Your short and focused explanation is great to understand. Edit: I added another view and wanted it to be the default view, but it doesn't work. Could you explain how to set a view as default?
You are a hero. This helped me a lot.
Very good video, made views make way more sense.
Finally Thanks dude... 🔥
Thanx for the great sample, I am looking to extend this, the question and task I have is to create multiple instances with code behind of the same view, and use the Obj instance to work dynamically with the instances (eg like the tabs in VS, all have same window, but the instances will save to different files, show different code and behave different, but using one and the same XAML page with code)
I hope u come back soon. Ur content is gold! ❤
Just the thing I needed..Thank You So Much!!!!!
Great video. Is there a follow up video showing how to pass data from parent to child and vice versa using MVVM?
Oh, this is a nice video tutorial. Thank you very much. This is exactly what I was looking for. Just out of curiosity, do you think this would work with anything other than a user control? Say, like a Page or something like that?
This was a great tutorial. Thank you
Hello ToskersCorner,
I love your tutorial, but unfortunately I am very bad in English and especially the audio, so I may be missing an information so me question.
Is there a need to close the old views?
For example when you switch from red to blue, red is always loaded? where it has been automatically closed.
I ask you this because, my views contain information that it will look for on a system (PLC) by an RS232 communication.
Each view will look for different information. If a page that has been opened remains open when you change your view, it will continue to search for data on the RS232 and overload the communication.
I hope you understand what I'm trying to say to you, sorry for my english.
Thanks for your feedback.
Great tutorial. Thank you.
thx guy. the best i´d ever seen
very helpful thank you!! I have a question about this, if i have a button in my red View and i want to change from there so the green view, is there a way to do it? or can i remove the data context from the red view ? Thanks a lot for your nice tutorial!
Thanks for the video, simple and easy to follow!
Awesome to hear!
thank you for this tutorial it worked and I am so happy
Great Video! Is there a way to pass parameters to the usercontroll when you click a button?
Thank you very much... Its really quite helpful...
This is a very unique tutorial and a useful one too.
Thank you Tosker @ToskersCorner
Very helpful. Thanks
thank you, man
No problem. I hope it was useful :)
Thank you so much lol. I've been trying to achieve this but couldn't figure out what it was called. I was trying with tabcontrol. Very much appreciated now I'll fix all my coding
Thanks for this, Tosker
Interesting. Why did you decide to split the grid up into a bunch of small cells? This sort of reminds me of Bootstrap's approach to responsive mobile UI development. Is that the goal?
Thank you very much, that i need. You're save my life...
Excellent!!
Fantastic video. Thank you.
helpful video. thanks sir
is it possible to change the view with a butten from ur current view(e.g from blue to red with a button in the red view ) without using the buttons on the menu bar?
Glad you asked. Until I am able to do a video on it, I made a post on my website to address your question. I also provided an example project on GitHub to show you how this is done. Link is in the description. :)
thx
The "BaseViewModel" is a base for all viewmodels EXCEPT the main. we bind our current view to. So when we bind our view to the "ViewModel" property in our MainViewModel, we can change the property(To a Red/Blue View Model) to change the view. Since Red/BlueViewModels are derived from BaseViewModel we are able to assign them to that property.
Your video was great help but it is more creating new views rather than switching between them. I'm part way there anyway :)
Thanks mate! great video.
Great video, do you happen to have any videos showing how to nest this functionality? For example, having the new view model display a set of buttons and then when those buttons are clicked, another view replaces that view.
Edit: I think I found what i needed. Sounds like NavigationService will better serve my purpose
thanks for this man. keep it up
hey, thanks for the video, I have a question, how to route from one view to the other?
Thank you very much for your help
Hello.Nice video. I've question. Can it be possible to put in one view button which open another one view?
I really appreciate your helping, sharing your skills. I wonder do you make a number of viewmodels classes instead 1 class? they seem like work all the same. I tried with one viewmodel class and it does work.
Also, I want to know how to set the default page without clicking any buttons.
I made multiple just to show how you theoretically would do this with different views/view models. Ideally in a real word scenario each view would be doing something different thus needing different view models.
Okay man, thanks a lot!
Thanks a lot, man!
nice tutorial
Iam new to wpf coding. One question I have. what happen to preview view when new view is called. is it still running in background?
when would you likely use this? view switching? real world examples? (a liitle too abstract to work out why you'd use it)
You use it pretty much any time you wish the user to navigate to different content. I good example could be my C#.Net Sales Monitoring Tool I have in my videos. Or even my Memory Game which has a little main menu.
For another example: In WinForms you normally would open a new window to accomplish these things. With WPF you can make the content of a window dynamic instead of creating new windows every time. Sort of like page navigation in your browser.
Awesome tutorial, I am just start learning WPF, i have qustion, how could I put different data to the multipe views, thanks a lot.
Hey, Gerry.
Sorry I didn't respond. I must have missed your comment! For more specific questions I ask that you email them to me at ToskersCorner@outlook.com ! I always check my email and respond as soon as possible. Thanks!
Awesome tutorial, but I do have a question, how is this performance wise? Because every time you hit the button a new instance of the usercontrol is created right? or I'm wrong here?
Rolo B. (1/2)Great question! Yes, this is creating a new instance each time, i suppose i should have emphasized this is a demonstration. If you had an application where doing this would cause worrisome performance penalties we would simply place the instances in a wrapper class and alternate the values without recreating them everytime. We also could store them in our XAML resources if we wanted to prevent codebehind as well.
This tutorial was simply showing the concept in an abstract, because I assume most people would need to adjust it to their needs anyways.
Rolo B. (2/2)However i want mention specifically that we are not creating new user controls, but rather new instances of their data contexts. So the problem lays upon that, not the user controls.
Nice ! Thanks you so much for the clarification! It was so useful!
Best solution
A tip: turn up the volume when you edit next time :)
Yeah, I am aware of this now. Thanks.
Thx works fine. Nice explination ;)
It's possible to do this in windows forms? Thanks!
Seemed great to me.
Hello! What are the benefits of using user controls instead of pages? In which cases would be better to use user controls and in which pages?
Hello, sorry for the late response. The benefits really are that user controls are designed to be, well, controls. They are designed for desktop applications, and allow you to create your own "control" view vs. opening another window. Pages, are primarily intended for a web application or browser application. So, in the end it is not much about "benefit" but more so based on what each is intentionally designed for.
Great vid!
Just wanna ask how you can pass a data to the desired view?
e.g.
A JSON string from a successful login from an web api?
A question: is it ok to always create new instance of viewmodel whenever user clicked the button? Great vid btw!
Generally no. It would be best to keep your ViewModel instance stored somewhere for future access if you plan and re-using it. Perhaps in a single instance, or a collection list of view models in a main-detail view.
Hi Tosker,
Is it possible to have two or multiple content control elements for one window? If possible, how DataContext shared for binding?
Thanks in Advance
Vino
I hate giving the answer, but it is true in almost anything programming related: It depends. If, for example, you want two navigation menus (Maybe a main nav, and side bar) you would want a 'MainViewModel' holding two different data context sets, and have them bind and call back to the parent data context.
Thank you very much! I have a question: How can I set a default view, so it can be shown when the main window is loaded?
You can add DataContext = new VM() in the constructor of the code behind, where VM() is the ViewModel in which you want to have as default. This causes that view to be opened when main window is loaded
@@AIgineer Thanks a lot!
Great video! Your tutorials are helping me doing my university projects :D
Can you please explain the view models need? That parte isnt clear on my mind
The "view model" is not necessarily "needed". This is just how you would go about it conducting an MVVM type pattern. However, the reason we need a class object(View model) is because the DataTemplate we display is based on the Type of it's DataContext (Which would be the view model) (See 9:45). We tell it, if the DataContext of our window is BlueViewModel, then we want it to display the BlueView. The only way it can know what view to display, is if it can determine what type of DataContext is current(BlueViewModel). Hope I cleared it up a bit!
Great Video!
Question - I'm new to WPF, but I wonder why do you need to create a ViewModel to translate between 2 different views? It would be much simpler to if the Click button event would create a new *view* and not a *viewmodel*, and then you wouldn't need the data templates (i.e. DataContext = new RedView(), instead of new RedViewModel() ). So are there any benefits for this? I understand you want ViewModels as intermediates between Views (=UI) and Models (=Business logic) - but shouldn't you let the views talk between themselves?
In my opinion, you should not let the "views" talk between themselves. However, this depends on what you what you consider "talking". What using data templates provide is reusability, flexibility, and extensibility. Also, models are not necessarily business logic, that is the job of your ViewModel. Models are simply, well.. models. They are essentially objects for structuring sets of information -- not manipulating them. However, to give you a better view(No pun intended) of my perspective:
Let's say the following is a directory of views:
Red View -> Blue View
Blue View -> Yellow View
Yellow View -> Red View
Orange View -> Blue View
Notice how we display a view multiple times in different ways? In your proposal we would have to hookup the code-behind of Red and Orange to do the same thing. Whereas if we already have a data template in our resources, we simply set its data context in the XAML and views change accordingly.
I will admit, it is understandable you would feel this way, since the view is using code-behind to set the data contexts in this video. However, we should remember the DataContext can be set through binding, or directly in our XAML. Your suggestion will work. There is nothing "wrong" about it. It is just that by using patterns appropriately, if makes development much easier when changes and updates are inevitably made. As you make applications in WPF, some of these suggestions and methods that seem overkill will start to appear very useful. It's an understanding that is usually better understood through experience rather than books and videos. Many times I have felt using some of these patterns was pointless, to then soon run into a problem it may have fixed.
Also, I would lastly add that there is sometimes a difference in doing what is "easy" and doing what is "simple". Sometimes things are only easy in the short term and not the long term, and it's up to you to evaluate the difference.
Good question, I hope I didn't make anything more confusing! As I try to remind people in some of my videos, I am not an 'authority' figure on these subjects. Just a normal guy with passion for programming. People should always use their own minds to filter and research things they learn -- from me and from anyone else.
Wow - thanks for the very thorough response!!! It's not taken for granted. I can't say I understood all, but I'm sure it will drip-in slowly.
FYI - stackoverflow.com/questions/16338536/mvvm-viewmodel-and-business-logic-connection
D. Refaeli not exactly sure what you are meaning by saying this link in response. Let me know!
As much as I want to learn WPF, I feel like its a losing battle trying to learn it. It is just so complex and I can't seem to find and resources that teach you from the ground up. Watching random videos here and there and reading forum posts just aren't cutting it.
is there any way to preserve the data that exist in one view? because when i click to change the view and come back to the same view i get the "Default" view
very good :)
What do I do If I want to go to another page from a button I have created from doing this?
thank you bro, and I am waiting your new tutorials~
Thank you! I'm unsure how much more I can really cover specifically with WPF that I haven't already. I am considering doing a small series on C# in general, to perhaps attract more 'beginners' to the channel. Though, I assume most of my subscriber base likely doesn't need such a series. I figured starting out with an easy series will help get me into the swing of things again once I find the time. If there is anything you are interested in that I have not already covered, please let me know.
How do you hand over data from the child VM to the parent? This is what I am wondering. There are cases in which you want to update a few options inside a dialog which should affect the data in your main window.
It depends on what you are doing. You could always have an observer pattern so the child can notify the parent of changes.
You can do this using RoutedCommand or AttachedEvents
what if I enter some data in RedView Datacontext and then go to Blue View and come back to Red View. Will it reset the DataContext? everytime you click on button.
Hey Tosker I have a question what would be the best way to handle Dynamic list of objects in a treeview in an mvvm way... For example say if I have an oject class and i want to everytime i expand an object node i hit the database to get another list of objects based on a parent key field and if i expand any of these objects i hit the database and do the same thing again..
Well, I'm not so sure you would want to be hitting the DB every time a tree view is having its children indexed. Unless it is particularly sensitive information, or too massive for memory to temporarily store, I would consider loading them all into an object using WPF's ObservableCollection for storage. Perhaps an Model that forms a sort of 2D array. Hard to really unpack an answer with a lot of considerations to be made.
ToskersCorner Its mainly because the child data is constantly changing in my case and like you said there's a lot of data
ToskersCorner Ive came up with a solution but trying to handle in a mvvm way is whats causing my issue
Rio The Hitman Unfortunately this question is too vague for a direct answer. you can email me at toskerscorner@outlook.com with more specifics and your project code so i can have a better look.
why the class, with the controls is not just ?
Is your microphone integrated within your keyboard? XD
Will you be doing more like these?
First, sorry this is about a 6 month delay in response -- must of missed this comment. Second, I do tutorials generally at requests of viewers. You can always email me for suggestions!
I found a similar way to do this and was wondering if there are pros or cons to this approach? I am new to wpf / xaml so am not sure.
loginWindow.Visibility = Visibility.Visible;
toolWindow1.Visibility = Visibility.Collapsed;
toolWindow2.Visibility = Visibility.Collapsed;
thank you man!!!!!!!!!!!!!!
thnkx i needed this
Hello, I'am still a noob. Is it ok if we bind differents resource type into differents views(UC). For exemple the red view manage customers, the blue view manage products,... And is it possible to define data-context into redViewModel because I don't want to put all the viewchanger(navigation) into de MainWindow.cs with RedClicked, BlueClicked,... Sorry for my english, thank you for your video !
Typically you would have a view and a view model, and break up these responsibilities as you see fit. You can also have othere DataContexts in other view models, depending upon how you are designing. Try checking out some of my series videos, such as the "Contact Book Series" if you want a better idea of updating and changing view content.