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.
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!
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)
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 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
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?
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.
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 tried this twice and every time I get to the part "where I sometimes run into this problem and have to rebuild the solution" I click on rebuild and now my .exe file is missing and I have to start over.
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.
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.
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.
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;
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.
It worked like a Charm but sudntly i started to get this error when trying to Initialize the new page: System.Windows.Markup.XamlParseException: 'Page can have only Window or Frame as parent.' Any ideas?
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.
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
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, 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!
bro when i change views on button click my window is no longer resizeable or draggable...and no other button clicks are registered my main view has custom chrome style and its data context is set to a view model file...
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.
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.
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.
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.
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.
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
Hi I added a label and a button to change the content of the label in the redview. Now in red view I changed the content's label by pressing the button and it change successfully but the problem is when i click blue view and come back to the red view the label has the original content not the content that i set from the button. I cannot retain the value of the view once i change the view. Do you have any suggestion for retaining the value.
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.
hey, how can i make a menubutton highlighted when i'm on that view? like when i press it it stays active or something, untill i press another menubutton.
Ok Guys... Can someone explain to me one thing: - if we are setting DataContext to an instance of ViewModel class, - we are binding DataContext of ContentControl and View to this How exactly our App knows it has to load particular view.xaml file? I mean this ViewModel.cs and View.xaml are not connected in any way with the code.
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 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 searched the entire youtube for this tutorial, finally someone who has done it the i expected
This saved me yesterday from breaking my keyboard because my university course didn't explain MVVM enough. Thank you.
Oh my god, just thank you. You did more for me in 19 minutes than lecturer in my university in 2 classes
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
Did a great job on the video. It's still very applicable today. Thank you for taking the time.
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.
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.
The best tutorial for MVVM
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 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
Thank you Sir! Much appreciated. Struggled a lot with this topic. Very clearly explained.
You are a genius, finally an actual useful tutorial
One of the best done videos I've ever seen! Thank you!
Extremely useful tutorial. Thank you man.
Excellent tutorial. Good job!
Dude this was super helpful, and super simple to follow. Exactly what I was trying to find haha. Thanks!
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.
Just what I needed. Thanks. Keep up the good work.
Many thanks, man. Very good explanations. Good job
Very good video, made views make way more sense.
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.
thank you for this tutorial it worked and I am so happy
You are a hero. This helped me a lot.
Just the thing I needed..Thank You So Much!!!!!
I hope u come back soon. Ur content is gold! ❤
Thanks for the video, simple and easy to follow!
Awesome to hear!
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!
This is a very unique tutorial and a useful one too.
Thank you Tosker @ToskersCorner
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?
This was a great tutorial. Thank you
Thank you very much... Its really quite helpful...
Great video. Is there a follow up video showing how to pass data from parent to child and vice versa using MVVM?
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)
Great tutorial. Thank you.
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?
Your video was great help but it is more creating new views rather than switching between them. I'm part way there anyway :)
Finally Thanks dude... 🔥
thx guy. the best i´d ever seen
Thank you very much, that i need. You're save my life...
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?
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
Great Video! Is there a way to pass parameters to the usercontroll when you click a button?
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?
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.
Very helpful. Thanks
Thanks for this, Tosker
Hello.Nice video. I've question. Can it be possible to put in one view button which open another one view?
thank you, man
No problem. I hope it was useful :)
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
I tried this twice and every time I get to the part "where I sometimes run into this problem and have to rebuild the solution" I click on rebuild and now my .exe file is missing and I have to start over.
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.
Thanks mate! great video.
Fantastic video. Thank you.
hey, thanks for the video, I have a question, how to route from one view to the other?
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.
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?
Excellent!!
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!
Thank you very much for your help
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;
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!
It worked like a Charm but sudntly i started to get this error when trying to Initialize the new page:
System.Windows.Markup.XamlParseException: 'Page can have only Window or Frame as parent.'
Any ideas?
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.
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
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.
Thx works fine. Nice explination ;)
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!
It's possible to do this in windows forms? Thanks!
Thanks a lot, man!
helpful video. thanks sir
bro when i change views on button click my window is no longer resizeable or draggable...and no other button clicks are registered
my main view has custom chrome style and its data context is set to a view model file...
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.
I made this code last night and tried launching it this afternoon and the code buttons just don't work anymore :(
nice tutorial
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
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.
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.
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.
Great vid!
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.
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!
Okay man, thanks a lot!
A tip: turn up the volume when you edit next time :)
Yeah, I am aware of this now. Thanks.
Hi I added a label and a button to change the content of the label in the redview. Now in red view I changed the content's label by pressing the button and it change successfully but the problem is when i click blue view and come back to the red view the label has the original content not the content that i set from the button. I cannot retain the value of the view once i change the view. Do you have any suggestion for retaining the value.
it's becuase a new instances of the viewmodels are being created each time the button is clicked.
What do I do If I want to go to another page from a button I have created from doing this?
Seemed great to me.
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.
hey, how can i make a menubutton highlighted when i'm on that view? like when i press it it stays active or something, untill i press another menubutton.
TheMidasTouch
Gooogle wpf style setters.
Also you can use blend to build states to alter control styles on mouse over etc.
Ok Guys... Can someone explain to me one thing:
- if we are setting DataContext to an instance of ViewModel class,
- we are binding DataContext of ContentControl and View to this
How exactly our App knows it has to load particular view.xaml file?
I mean this ViewModel.cs and View.xaml are not connected in any way with the code.
They're connected through the datacontext
Is your microphone integrated within your keyboard? XD