I strongly recommend checking out my series on navigation. Not only is the video quality much better, but the approach I use in that series is better-suited for larger applications, and I also cover other navigation scenarios. Link: ruclips.net/p/PLA8ZIAm2I03ggP55JbLOrXl6puKw4rEb2
This is a great resource on MVVM. At first I was using frames to attempt switching views but this is much simpler as well as trying to use pages instead of only UserControls, cheers for the tutorial. Saved me at least a week or so figuring this out!
Notice that you need to Add UserControl(WPF) for the Home and Account Views. I was struggling with that since I created Page(WPF) But great Tutorial very helpful, thanks.
No problem Виктор, I'm glad you were able to solve your issues with the namespace. Trying a rebuild was a good idea. I'll suggest that next time someone has namespace issues, thanks!
Dear colleague, I developed to now web applications. But I didn't have any idea and it was different to understand before your tutorials. Now I am writing your codes line to line and trying to understand every points. Thank you so much about these resources.
@@SingletonSean Thank you by no means I didn't want to tell bad about Tim, and should spent 20 years before I can do so... on the other hand, your videos are really great. just the thing i wanted to mention is as always popular doesn't mean good... I just found your videos totally by chance... On the other hand wish your videos to gain deserver popularity soon...
7:14 So you always create new objects on command execution, I gues it is better to check if model is null then create one and store or smth like this, or I'm wrong?
Hello, how would you go about implementing this and then giving the ability to change the view depending on clicking/mousedown an element inside the ContentControl?
@SingletonSean if I want instead of the main page button use a link inside the Home what do I need to do ? at the moment I have both and mainpage btn works but on the link says on console that cant find the file
But if the DataContext is set to the MainViewModel, how do I let my views access my other view models? Because otherwise, it seems I have to cramp everything into one MainViewModel.
What if i wanted to open another view within a view? For instance, on my dashboard I have a customers tab. I click that and im taken to a view where the datagrid of customers is shown. But i want to have a button on that page that displays a new view for me to add a customer.
Hey T Pops, that's a great question. It seems like your application has some pretty advanced navigation. For advanced navigation in WPF, I would recommend wrapping your "SelectedViewModel" in a class, such as "Navigator". Then, I would bind your ContentControl on the MainWindow to the Navigator.SelectedViewModel. Now that your SelectedViewModel is in it's own class (Navigator), you could pass your Navigator through the constructor of your other view models (such as your CustomerViewModel). Then, in your CustomerViewModel, you could use the Navigator and change the SelectedViewModel to whatever you want (such as NewCustomerViewModel). In my Full Stack WPF MVVM series, I demonstrate wrapping the SelectedViewModel in it's own Navigator class. This video focused on simple navigation, so perhaps I will dedicate a video to advanced navigation and demonstrate what you are trying to accomplish. Thanks for the idea! I apologize if my written description didn't help, but keep an eye out for a video in the future.
Great tutorial!! Just have a question about stackpanel, I've put the buttons at the top of the window and I want the loaded page data to be displayed right below the buttons, how can I accomplish this?
Hello! Thank you for what you doing! I have a question. As programmer i always hate to repeat similiar code. For me "DataTemplate" block is excess. Is any opportunity to bind view and viewModel automatically? Like if i have HomeViewModel - so i need just HomeView. Thats it. Any suggestions?
I got the error "The event "UpdateViewCommand.CanExecuteChanged" is never used. I just copied your project and added/renamed the views and viewmodels. Can someone help?
Really nice tutorial and i was able to build the demo but now i have a strange issue. If i have a button in the MainWindow it works correctly. But if the button is on the Usercontrol that i loaded the ContentControl does not get updated even though the Updateviewcommand is fired and viewmodel.SelectedViewModel is set Any idea why that is?
I want buttons on my child views to switch contexts, but I wasn't able to get that to work. I tried putting the thing that stores the UpdateViewCommand function in the parent class and have the child view models inherit it, but that doesn't work. What should I do to achieve this objective? EDIT: when binding a function to a button, I have to bind the name that the viewmodel gave it, not the name of the class itself.
Honestly a lifesaver. Problem is, build order stings a bit. If I wanted to add more views I need to change the csproj or remember to build before altering the App.xaml file.
Hi ElCidPhysics! The MainViewModel will indeed need to be a view model (implement INotifyPropertyChanged) since we bind to it from the view. Without implementing INotifyPropertyChanged, the view would not update when we switch views. There's also been discussion that binding to an object that doesn't implement INotifyPropertyChanged could cause memory leaks, but I've never seen it first-hand.
@@SingletonSean awesome. Thanks for the explanation. Learning WPF has been a struggle. I get WinForm, .Net, even Blazor to an extent. Bug fir whatever reason I’m struggling with WPF. Thanks for your videos.
I'm making an app that has a dashboard as the home screen, I want that to show at startup, so should I even have a dashboard view and just design that on the mainwindow?
Hello I tried in my mvvm project..but command is not getting fired from mainwindow though I given proper command and it's parameter...even debugger is not coming when I click home or account
Hi, I found the following error, System.Windows.Markup.XamlParseException: 'Window must be the root of the tree. Cannot add Window as a child of Visual.'
Hi @SingletonSean I just started learning WPF. I was working on a personal project. I want to use two commands in a SIngle ViewModel. Can you suggest me how I do that or refer me a video?
Thanks rcs368, I had another viewer suggest I increase the font size as well. If you look at my recent videos, they should have larger fonts, but let me know if the font size still is too small. I apologize for the small fonts on my older videos, I was a rookie lol.
Okay so I am having a funny issue now. It used to work from App.xaml . Now when I bind to a new instance to the 's Content property I can't open the respective views. But when I add
Gives an error in the App.xaml file at saying - " The HomeViewModel does not exist in the namespace " clr-namespace:MMVMTesting2.ViewModels ". Any idea if what would cause this Error?
Hey Ma Jordan, thanks for watching! The DataTemplate tells WPF what to display for a certain type. For example, at 2:27 we create a ContentControl and set the Content property to our SelectedViewModel. At 4:05, I use a DateTemplate to basically tell WPF that HomeViewModels should be displayed as HomeViews. As a result, when the type of the Content property on the ContentControl is a HomeViewModel, the ContentControl will display a HomeView.
Hey, when I press a button nothing happens, and this is in my output: System.Windows.Data Error: 40 : BindingExpression path error: 'UpdateViewCommand' property not found on 'object' ''Login1ViewModel' (HashCode=31450711)'. BindingExpression:Path=UpdateViewCommand; DataItem='Login1ViewModel' (HashCode=31450711); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand') How can I fix this? Basically, I did the same thing as you, except that my button is not in MainWindow, but in Login1View instead.
Hi Rain, the issue is that your DataContext for your button is the Login1ViewModel. Therefore, your UpdateViewCommand will need to be on your Login1ViewModel as well. Otherwise, your button will be unable to find the command. It seems that you are attempting to navigate to a different view from within another view, correct? I have many people asking about this, so perhaps I will make a video on solving this navigation case.
@@SingletonSean Still haven't managed to fix it :( Do you perhaps have an example of this somewhere? I'm getting some weird errors and I don't even know where to begin debugging it.
That is true Limbique. You would also extract all the viewmodel-to-view mappings to their own ResourceDictionary so that they are easier to find. Then you'd just need to import the ResourceDictionary into the App.xaml. I usually do this whenever I have a ton of views/view models.
That's correct Tacci, those are new VS features. I used to always have to work in VS 2013 in legacy projects :( If you're unsure, the alternative to nameof() is simply the name of the property as a string. The alternative to PropertyChanged?.Invoke is to null check PropertyChanged, and then call PropertyChanged like a method. Thanks for watching!
@@SingletonSean ye i already found out, but my view is still not changing, even tho my button is working , perhaps its because i binded the SelectedViewmodel as ContentControl on the main window, but i cant bind it as Content Control on my user COntrol
Thanks for the video, very simple and straightforward to understand. However I was wondering why didn't you use pages instead of user control for views? Is there any special considerations while using pages instead of user control?
Hey Iftaker, that's a great question. Pages are most beneficial along with NavigationWindows. Since I typically just use a plain Window, I almost always use UserControls. EDIT: In fact, here's a good SO comment that talks about Window vs. Page vs. UserControl: stackoverflow.com/a/20292416
Thanks Rafael! Many people ask about my experiences with WPF frameworks. I haven't used Caliburn before on a large app, but I have played around with it and browsed through the docs. I'm not a big fan of the framework's conventions, and I also noticed the project is no longer under maintenance. I feel more comfortable using WPF without a framework. I've read many varying opinions on WPF frameworks, so I don't think there's any right or wrong option.
How are you doing this without a page in a frame? I followed word for word, even paused to make sure our code was the same, but it keeps yelling at me that a page must be part of a frame when I try to run.
Beat me to it! That's correct, UserControls instead of Pages. Great job exploring and solving the problem on your own. Thanks for watching Jesse, glad the video helped.
After I tried the code, I found every time when I clicked the button, it will create another instances. That means if I click 10 times Home or Account button, it will create 10 instances of Home view and Account view. Did I miss anything?
this is great many thanks can you show us how to use enum in commandparameters instead of normal text how to open new window and collect data from it using MVVM thanks in advance
Sorry did not leave a contact Hello, I found your video very useful. How could I change the button(s) and put a treeview control in place to change the views. Also in the TreeView I would like to have a just one known Parent Node (by Name) have a contextmenu that adds a child. Then in the Child Nodes of this particular Parent Node have a different ContextMenu that has two options Rename and Delete. One thing to note with the known Parent Node is that the Child Node(s) all use the same view and only the data changes. The structure would be:- +Root +Parent1 Child1 {view UserControl1} Child2 {view UserControl2} +Parent2 Child1 {view UserControl3} Child2 {view UserControl4} +ParentX {ContextMenu(P) - Add Child} ChildX1 {ContextMenu(C) - Rename Child, Delete Child} {view UserControlX} ChildX2 {ContextMenu(C) - Rename Child, Delete Child} {view UserControlX} ChildX3 {ContextMenu(C) - Rename Child, Delete Child} {view UserControlX} +Parent3 Child1 Child2 Can you help me please or point me in the correct direction. Bates.JamesDouglas@gmail.com Show less
Thanks for watching Day! Unfortunately, I don't have a specific video on this, but have this in the plans for a really awesome series. I think what you might be looking for is the concept of a "store". The store holds shared application state, and then the view models use the store to grab the shared data. This is inspired by the "mediator" design pattern. I think my best example is in my SimpleTrader application, specifically with these files: AssetStore (shared properties) github.com/SingletonSean/SimpleTrader/blob/master/SimpleTrader/SimpleTrader.WPF/State/Assets/AssetStore.cs AssetSummaryViewModel (uses AssetStore for shared properties) github.com/SingletonSean/SimpleTrader/blob/master/SimpleTrader/SimpleTrader.WPF/ViewModels/AssetSummaryViewModel.cs
Did you import the views namespace into your App.xaml? Is your HomeView in the namespace "views"? If you post a link to your code, I will be able to diagnose the issue.
Hey Nicolás, great question. I actually get that question a lot, so I ended up making a demo last time someone asked. Take a look at this: github.com/SingletonSean/SimpleTrader/commit/428c6de1c52e4973495dc8ab4837a839050c886c The main idea is that you store the view model in a field and set the SelectedViewModel to the view model in the field whenever you want to switch back to the view. Let me know if you have any issues :)
Bro I'm not yet done with the whole video but I know that this is exactly what I was looking for. This is the very basic that I need to continue your SimpleTrader playlist currently on part 10 but I need to go back to get the basic understood well. PS. The financialprep api for ticker price cannot be accessed anymore if you are not a premium member. I'm having a hard time how to catch up with your tutorial if some of the api endpoints cannot be accessed anymore.
Glad this is helpful Alvin! As for the SimpleTrader playlist, sadly there's not much we can do about the API being premium only now. However, I'd recommend checking out this tutorial! ruclips.net/p/PLA8ZIAm2I03hS41Fy4vFpRw8AdYNBXmNm It's much more concise and covers WPF features better imo.
@@SingletonSean Thank you for the recommendation. This would be a perfect time to spend my weekend watching and learning from this playlist you provided. So far you both Tim Corey and You are the real OG of C# for me not joking 😤
Hello David, I will be able to solve your issue if you provide source code. However, are there any errors in the output window when debugging? Did you map your views to view models in your App.xaml? Is anything being displayed on the screen (such as the names of the view model classes)?
Hello, You have to to the following Things: - in our MainWindow.xaml.cs File you have to add "DataContext = new MainViewModel();" in your Constructor - your AView.xaml.cs and BView.xaml.cs Files are missing sorry for my bad English :)
Hey Yanzhai, glad the video helped. Personally, I have never felt the need for an MVVM framework as I feel WPF has good support for MVVM, but many people have requested I try out a framework. I always feel like I learn something useful from trying frameworks, so I might take a look.
@@SingletonSean Yes. this (gyazo.com/6fa4296371a2323aad0425d9e5a602af) is my MainWindow at launch. When I start the app, I would like for the Dashboard View to be shown by default where the white space is. I have already set that area of the screen for the content control, I'm just wondering how to display this particular View by default without having to click the Button for it after startup. (highlight link and copy, clicking doesnt show image). Thanks for the help!
Does the UpdateViewCommand execute from your constructor? Have you tried walking through/debugging with breakpoints and seeing if the SelectedViewModel is being set?
It is a great tutorial but for me the previous View does not disappears on the button click it puts the new content on top of the other I'm sure I missed something but can't find what 😵💫
I'm looking for tutorial like this for a while. I just want to ask you how can I build the same project using .Net Framework (Not .Net core)? and how to use MVVM inside my current project(I'm using .Net Framwork 4.6) ?...Thank you bro in advance.
Problem ist that this doesn't work in .Net Core for any reason (no problem in .Net Framework). Edit: It DOES work in .Net when you switch the core version from 6.0 to 5.0 (and then back to 6.0). Maybe it's one of the Visual Studio's hiccups
I like using CallerMemberName, but some projects I work in don't use it :( so I just always pass in the property name as a habit in case the project doesn't use CallerMemberName. Plus, I created a snippet that generates all the code for an OnPropertyChanged property, so it doesn't bug me much. For personal projects I should just use it though lol. Thanks for the suggestion Afterbunny!
Hey kumar, my newer videos have the increased font size. Check out this series if you want to see navigation videos with increased font size :) ruclips.net/p/PLA8ZIAm2I03ggP55JbLOrXl6puKw4rEb2
Very complicated and much code only for changing content. Why not making CunstomControls as Form and exchange them in DataContext on Mainform? That also needs no PropertyChanged for Mainform.
Hey mb, thanks for watching and glad to hear you enjoyed the video. This is WPF, so unfortunately I'm not sure what you mean by form. I also prefer keeping my navigation in the view model so I can navigate from inside a view model whenever I need to. However, I'm always interested in seeing other people's ideas in action, so it would be awesome if you linked to an implementation of your solution if possible.
@@SingletonSean This is what I hate about WPF, much code for less things. But I must excuse myself, watching it on 2 times speed was too fast. You did it right and in correct way. Maybe you can save the ICommand by using code-behind with that click-event. But as always there are hundreds ways to solve a problem. Also I never used a Property-Changed-Event for that, reassigning the data-context did an update by itself, if I remember right. With "Mainform" I ment MainWindow., you also still used a UserControl, so please excuse my mistake. But otherwise a very good tutorial and very good explained! I myself experimented with a more crazy way, I made every model-view static and let them handle his Window by itself. Exchanging data between different view is easy that way, but it takes a little bit of that separation between UI and Code, but for most applications a correct done MVVM is way to much work to implement, so I tried a way between.
Hey mb, thanks for the feedback, I love discussing solutions and those are good points. I agree, could save an ICommand implementation or even use the "RelayCommand" that everyone seems to love, but like you said hundreds of ways to solve a problem, hahaha. Anyways, you're definitely right that it does take a lot of code to do some things in WPF, and making that choice really depends on the scope of your project. Like you suggested, many simple projects probably don't need to spend time implementing a pure MVVM solution. I'm sure you're not the only one who thinks this takes too much code, so I would like to revisit my implementation and see if I could simplify the solution. I've explored a lot with navigation since I made this video.
I strongly recommend checking out my series on navigation. Not only is the video quality much better, but the approach I use in that series is better-suited for larger applications, and I also cover other navigation scenarios.
Link: ruclips.net/p/PLA8ZIAm2I03ggP55JbLOrXl6puKw4rEb2
This is a master piece code, explained in the most simplest form ....Thanks a lot from Zimbabwe
Thanks for watching Tatenda, glad you enjoyed!
This is a great resource on MVVM. At first I was using frames to attempt switching views but this is much simpler as well as trying to use pages instead of only UserControls, cheers for the tutorial. Saved me at least a week or so figuring this out!
well, this is the best video explaining navigation using MVVM so far, easy and simple, thank you verry much sir, you should keep doing this, nice work
Thank you abdelhalim, glad you enjoyed!
Thank you, highly helpful video (once I got that views here are usercontrols, not pages). Very understandable, very pleasant manner of explaining.
this is now all tutorials should be done, great work dude
You are amazing.. This is the best informative series of videos on RUclips.
Thanks Oualid, that means a lot! I'm glad these vids are helpful
Notice that you need to Add UserControl(WPF) for the Home and Account Views. I was struggling with that since I created Page(WPF)
But great Tutorial very helpful, thanks.
Thank you so much. Your video makes it very clear how MVVM works. Thanks again. Hi from Belarus.
10 / 10.
No problem Виктор, I'm glad you were able to solve your issues with the namespace. Trying a rebuild was a good idea. I'll suggest that next time someone has namespace issues, thanks!
Dear colleague,
I developed to now web applications. But I didn't have any idea and it was different to understand before your tutorials. Now I am writing your codes line to line and trying to understand every points.
Thank you so much about these resources.
Excellent tutorial - i have watched videos of tim corey - you can expect 20 minutes of this video more valuable than 20 hours of tim's.
Thanks Mehdi! I do prefer to keep my content short and concise. Tim's content is great too!
@@SingletonSean Thank you by no means I didn't want to tell bad about Tim, and should spent 20 years before I can do so... on the other hand, your videos are really great. just the thing i wanted to mention is as always popular doesn't mean good... I just found your videos totally by chance... On the other hand wish your videos to gain deserver popularity soon...
Your channel is way underrated.
Thanks ElCidPhysics!!
@@SingletonSean You're welcome.
7:14 So you always create new objects on command execution, I gues it is better to check if model is null then create one and store or smth like this, or I'm wrong?
Hello, how would you go about implementing this and then giving the ability to change the view depending on clicking/mousedown an element inside the ContentControl?
Thank you, that helped a lot. I did it a bit different with a switch case and even more strict separations/ abstractions and it worked :)
Excellent video. Thank you for sharing this very important concept.
@SingletonSean if I want instead of the main page button use a link inside the Home what do I need to do ?
at the moment I have both and mainpage btn works but on the link says on console that cant find the file
I found the answer have to look for ancestors
But if the DataContext is set to the MainViewModel, how do I let my views access my other view models? Because otherwise, it seems I have to cramp everything into one MainViewModel.
Excellent!! This what I have been trying to do. Thank you.
saved my life. thanks for this awesome video!
What if i wanted to open another view within a view? For instance, on my dashboard I have a customers tab. I click that and im taken to a view where the datagrid of customers is shown. But i want to have a button on that page that displays a new view for me to add a customer.
Hey T Pops, that's a great question. It seems like your application has some pretty advanced navigation. For advanced navigation in WPF, I would recommend wrapping your "SelectedViewModel" in a class, such as "Navigator". Then, I would bind your ContentControl on the MainWindow to the Navigator.SelectedViewModel.
Now that your SelectedViewModel is in it's own class (Navigator), you could pass your Navigator through the constructor of your other view models (such as your CustomerViewModel). Then, in your CustomerViewModel, you could use the Navigator and change the SelectedViewModel to whatever you want (such as NewCustomerViewModel).
In my Full Stack WPF MVVM series, I demonstrate wrapping the SelectedViewModel in it's own Navigator class. This video focused on simple navigation, so perhaps I will dedicate a video to advanced navigation and demonstrate what you are trying to accomplish. Thanks for the idea! I apologize if my written description didn't help, but keep an eye out for a video in the future.
SingletonSean Thank you!
@@SingletonSean Did you manage to upload that video? It would be helpful! Thank you:)
Great tutorial!!
Just have a question about stackpanel, I've put the buttons at the top of the window and I want the loaded page data to be displayed right below the buttons, how can I accomplish this?
Hello! Thank you for what you doing! I have a question. As programmer i always hate to repeat similiar code. For me "DataTemplate" block is excess. Is any opportunity to bind view and viewModel automatically? Like if i have HomeViewModel - so i need just HomeView. Thats it. Any suggestions?
At 3:50, i have no DataContext found for SelectedViewModel, can anyone help me w this? Much apperciate.
@SingletonSean just out of curiosity are you using the WPF Core templates for these tutorials?
I got the error "The event "UpdateViewCommand.CanExecuteChanged" is never used. I just copied your project and added/renamed the views and viewmodels. Can someone help?
I like it but it does not work when I try to change the SelectedViewModel within another view =(
Really nice tutorial and i was able to build the demo but now i have a strange issue. If i have a button in the MainWindow it works correctly.
But if the button is on the Usercontrol that i loaded the ContentControl does not get updated even though the Updateviewcommand is fired and viewmodel.SelectedViewModel is set
Any idea why that is?
Nice video! It is that i loocked for!
What WPF version are you doing?
I want buttons on my child views to switch contexts, but I wasn't able to get that to work. I tried putting the thing that stores the UpdateViewCommand function in the parent class and have the child view models inherit it, but that doesn't work. What should I do to achieve this objective?
EDIT: when binding a function to a button, I have to bind the name that the viewmodel gave it, not the name of the class itself.
Very usefull video! Thanks a lot!
what does the DataTemplate do?
Honestly a lifesaver. Problem is, build order stings a bit. If I wanted to add more views I need to change the csproj or remember to build before altering the App.xaml file.
Does MainViewModel need to be a viewmodel? Could it be just be just a c# class with a field or property that is the selectedViewModel?
Hi ElCidPhysics! The MainViewModel will indeed need to be a view model (implement INotifyPropertyChanged) since we bind to it from the view. Without implementing INotifyPropertyChanged, the view would not update when we switch views. There's also been discussion that binding to an object that doesn't implement INotifyPropertyChanged could cause memory leaks, but I've never seen it first-hand.
@@SingletonSean awesome. Thanks for the explanation. Learning WPF has been a struggle. I get WinForm, .Net, even Blazor to an extent. Bug fir whatever reason I’m struggling with WPF. Thanks for your videos.
No worries, it is certainly notorious for having a steeper learning curve 😅
thank you, sir. simple and powerful code
thanks, great starting point, but what if i only want to switch between the views and not having a new instance every time i switch?
I'm making an app that has a dashboard as the home screen, I want that to show at startup, so should I even have a dashboard view and just design that on the mainwindow?
Thank you very much for the video! :)
how am i going to put material design theme resources and data templates together ....i get errror
I barely see your screen. could you post some source code? thanks
Hello I tried in my mvvm project..but command is not getting fired from mainwindow though I given proper command and it's parameter...even debugger is not coming when I click home or account
love your work! thanks for your effort.
Awesome tutorial!
Hi, I found the following error,
System.Windows.Markup.XamlParseException: 'Window must be the root of the tree. Cannot add Window as a child of Visual.'
Same ...
Your views have to be of type "User Control", not "Window"!
What if my viewmodels already inherit from another class?/ I can not implement BaseViewModel to them :(
Hi @SingletonSean
I just started learning WPF. I was working on a personal project. I want to use two commands in a SIngle ViewModel. Can you suggest me how I do that or refer me a video?
Yes Ritam! I discuss this topic in this video (at around the 7:05 mark): ruclips.net/video/bYPU8X8f2xU/видео.html
Thank you very much.
I like what you are doing here, any chance you could zoom in a little, or increase your font size in the editor for video purpose?
Thanks rcs368, I had another viewer suggest I increase the font size as well. If you look at my recent videos, they should have larger fonts, but let me know if the font size still is too small. I apologize for the small fonts on my older videos, I was a rookie lol.
@@SingletonSean I noticed on the next video I watched the font size was MUCH better. Thanks
Great! Thanks for the feedback.
hi do you have a github repo for this demo?
I know this is easy, but, how can I show one of this pages at the start of the application?
Okay so I am having a funny issue now. It used to work from App.xaml . Now when I bind to a new instance to the 's Content property I can't open the respective views. But when I add
Gives an error in the App.xaml file at saying - " The HomeViewModel does not exist in the namespace " clr-namespace:MMVMTesting2.ViewModels ".
Any idea if what would cause this Error?
Nevermind, just needed to Build the application and the issue was resolved
Awesome Robert, way to persevere through your issues!
Hello, thanks for your video.
What does the DataTemplate have to be built?
Hey Ma Jordan, thanks for watching! The DataTemplate tells WPF what to display for a certain type. For example, at 2:27 we create a ContentControl and set the Content property to our SelectedViewModel. At 4:05, I use a DateTemplate to basically tell WPF that HomeViewModels should be displayed as HomeViews. As a result, when the type of the Content property on the ContentControl is a HomeViewModel, the ContentControl will display a HomeView.
Hey, when I press a button nothing happens, and this is in my output:
System.Windows.Data Error: 40 : BindingExpression path error: 'UpdateViewCommand' property not found on 'object' ''Login1ViewModel' (HashCode=31450711)'. BindingExpression:Path=UpdateViewCommand; DataItem='Login1ViewModel' (HashCode=31450711); target element is 'Button' (Name=''); target property is 'Command' (type 'ICommand')
How can I fix this? Basically, I did the same thing as you, except that my button is not in MainWindow, but in Login1View instead.
Hi Rain, the issue is that your DataContext for your button is the Login1ViewModel. Therefore, your UpdateViewCommand will need to be on your Login1ViewModel as well. Otherwise, your button will be unable to find the command.
It seems that you are attempting to navigate to a different view from within another view, correct? I have many people asking about this, so perhaps I will make a video on solving this navigation case.
@@SingletonSean Yes, that is what I'm trying to do. A video would be great :)
@@SingletonSean Still haven't managed to fix it :( Do you perhaps have an example of this somewhere? I'm getting some weird errors and I don't even know where to begin debugging it.
@@rain2223 could you fix it now?
Does it mean that for each new type of viewmodel, you need to alter the app.xaml?
That is true Limbique. You would also extract all the viewmodel-to-view mappings to their own ResourceDictionary so that they are easier to find. Then you'd just need to import the ResourceDictionary into the App.xaml. I usually do this whenever I have a ton of views/view models.
Hello, i find your videos really helpful but i have a question, how would I go if i want to change the view from another ViewModel?
nameof() doesnt work for me working with visuals stuido 2013, same goes for ProperyChange?.Invoke
That's correct Tacci, those are new VS features. I used to always have to work in VS 2013 in legacy projects :(
If you're unsure, the alternative to nameof() is simply the name of the property as a string. The alternative to PropertyChanged?.Invoke is to null check PropertyChanged, and then call PropertyChanged like a method. Thanks for watching!
@@SingletonSean ye i already found out, but my view is still not changing, even tho my button is working , perhaps its because i binded the SelectedViewmodel as ContentControl on the main window, but i cant bind it as Content Control on my user COntrol
Thanks for the video, very simple and straightforward to understand. However I was wondering why didn't you use pages instead of user control for views? Is there any special considerations while using pages instead of user control?
Hey Iftaker, that's a great question. Pages are most beneficial along with NavigationWindows. Since I typically just use a plain Window, I almost always use UserControls.
EDIT: In fact, here's a good SO comment that talks about Window vs. Page vs. UserControl: stackoverflow.com/a/20292416
How can i display a usercontrol by clicking a button in another usercontrol ?
Good teacher! bty, do you use Caliburn in bigger apps?
Thanks Rafael! Many people ask about my experiences with WPF frameworks. I haven't used Caliburn before on a large app, but I have played around with it and browsed through the docs. I'm not a big fan of the framework's conventions, and I also noticed the project is no longer under maintenance. I feel more comfortable using WPF without a framework. I've read many varying opinions on WPF frameworks, so I don't think there's any right or wrong option.
How are you doing this without a page in a frame? I followed word for word, even paused to make sure our code was the same, but it keeps yelling at me that a page must be part of a frame when I try to run.
Never mind. I believe I discovered they are UserControls, not pages. Good tutorial to show the MVVM framework.
Beat me to it! That's correct, UserControls instead of Pages. Great job exploring and solving the problem on your own. Thanks for watching Jesse, glad the video helped.
I have a problem of data lost in views when i switch views then views can't hold data
Github link if possible.
I think I am doing something wrong in views folder content
After I tried the code, I found every time when I clicked the button, it will create another instances. That means if I click 10 times Home or Account button, it will create 10 instances of Home view and Account view. Did I miss anything?
That is correct Alex! The garbage collector will cleanup the previous 9 instances of views/view models, so no need to worry about memory leaks.
@@SingletonSean Thanks for your reply. :)
Great tutorial. It's only 50% of my problem. How do you pass data objects from MainViewModel to the new view ?
Good tutorial, thanks!
Thanks Danger418, glad you enjoyed it!
this is great
many thanks
can you show us how to use enum in commandparameters instead of normal text
how to open new window and collect data from it using MVVM
thanks in advance
Sorry did not leave a contact
Hello, I found your video very useful. How could I change the button(s) and put a treeview control in place to change the views. Also in the TreeView I would like to have a just one known Parent Node (by Name) have a contextmenu that adds a child. Then in the Child Nodes of this particular Parent Node have a different ContextMenu that has two options Rename and Delete. One thing to note with the known Parent Node is that the Child Node(s) all use the same view and only the data changes. The structure would be:-
+Root
+Parent1
Child1 {view UserControl1}
Child2 {view UserControl2}
+Parent2
Child1 {view UserControl3}
Child2 {view UserControl4}
+ParentX {ContextMenu(P) - Add Child}
ChildX1 {ContextMenu(C) - Rename Child, Delete Child} {view UserControlX}
ChildX2 {ContextMenu(C) - Rename Child, Delete Child} {view UserControlX}
ChildX3 {ContextMenu(C) - Rename Child, Delete Child} {view UserControlX}
+Parent3
Child1
Child2
Can you help me please or point me in the correct direction.
Bates.JamesDouglas@gmail.com
Show less
you are my god
Thanks for creating this tutorial =)
Do you have an example sharing property among ViewModels, for instance,, ListView => DetailsView
Thanks for watching Day! Unfortunately, I don't have a specific video on this, but have this in the plans for a really awesome series. I think what you might be looking for is the concept of a "store". The store holds shared application state, and then the view models use the store to grab the shared data. This is inspired by the "mediator" design pattern. I think my best example is in my SimpleTrader application, specifically with these files:
AssetStore (shared properties)
github.com/SingletonSean/SimpleTrader/blob/master/SimpleTrader/SimpleTrader.WPF/State/Assets/AssetStore.cs
AssetSummaryViewModel (uses AssetStore for shared properties)
github.com/SingletonSean/SimpleTrader/blob/master/SimpleTrader/SimpleTrader.WPF/ViewModels/AssetSummaryViewModel.cs
Why
write "The type views:HomeView was not found "?
Did you import the views namespace into your App.xaml? Is your HomeView in the namespace "views"? If you post a link to your code, I will be able to diagnose the issue.
@@SingletonSean Thank you ) I resolved it . Just re-build)
Anyone can tell me how command in Views knows about the ViewModel?
Thank you so much bro!
Thanks for watching Nicolás!
@@SingletonSean Hey, I have a question. How I can switch panels but with a single instance of each one?
Hey Nicolás, great question. I actually get that question a lot, so I ended up making a demo last time someone asked. Take a look at this:
github.com/SingletonSean/SimpleTrader/commit/428c6de1c52e4973495dc8ab4837a839050c886c
The main idea is that you store the view model in a field and set the SelectedViewModel to the view model in the field whenever you want to switch back to the view. Let me know if you have any issues :)
Bro I'm not yet done with the whole video but I know that this is exactly what I was looking for. This is the very basic that I need to continue your SimpleTrader playlist currently on part 10 but I need to go back to get the basic understood well.
PS. The financialprep api for ticker price cannot be accessed anymore if you are not a premium member. I'm having a hard time how to catch up with your tutorial if some of the api endpoints cannot be accessed anymore.
Glad this is helpful Alvin! As for the SimpleTrader playlist, sadly there's not much we can do about the API being premium only now. However, I'd recommend checking out this tutorial! ruclips.net/p/PLA8ZIAm2I03hS41Fy4vFpRw8AdYNBXmNm
It's much more concise and covers WPF features better imo.
@@SingletonSean Thank you for the recommendation. This would be a perfect time to spend my weekend watching and learning from this playlist you provided.
So far you both Tim Corey and You are the real OG of C# for me not joking 😤
Please suggest @singleton sean
can you share the code
I've tried this method and I'm not getting anything showing where the view should be :/
Hello David, I will be able to solve your issue if you provide source code. However, are there any errors in the output window when debugging? Did you map your views to view models in your App.xaml? Is anything being displayed on the screen (such as the names of the view model classes)?
@@SingletonSean I've completely recreated the project practically ad verbatum and I see no errors and nothing displayed on the screen.
I would love to help, but I cannot give suggestions or feedback without seeing your current project.
@@SingletonSean www.sendspace.com/file/q3boey Here is the project. I would much appreciate it if you could take a look.
Hello,
You have to to the following Things:
- in our MainWindow.xaml.cs File you have to add "DataContext = new MainViewModel();" in your Constructor
- your AView.xaml.cs and BView.xaml.cs Files are missing
sorry for my bad English :)
Hello, your video is great help for me to learn wpf. Hope some mvvm framework videos,such as mvvmcross or prism. Thanks
Hey Yanzhai, glad the video helped. Personally, I have never felt the need for an MVVM framework as I feel WPF has good support for MVVM, but many people have requested I try out a framework. I always feel like I learn something useful from trying frameworks, so I might take a look.
@@SingletonSean Take a look at ReactiveUI
Let's say you wanted your home view to show at startup, how would that work?
Hey Tanner, have you tried simply executing the UpdateViewCommand from your MainViewModel constructor?
@@SingletonSean Yes. this (gyazo.com/6fa4296371a2323aad0425d9e5a602af) is my MainWindow at launch. When I start the app, I would like for the Dashboard View to be shown by default where the white space is. I have already set that area of the screen for the content control, I'm just wondering how to display this particular View by default without having to click the Button for it after startup. (highlight link and copy, clicking doesnt show image). Thanks for the help!
Does the UpdateViewCommand execute from your constructor? Have you tried walking through/debugging with breakpoints and seeing if the SelectedViewModel is being set?
@@tpops2697 try add back the "new HomeViewModel(); in the MainViewModel
nice thank you :)
It is a great tutorial but for me the previous View does not disappears on the button click it puts the new content on top of the other I'm sure I missed something but can't find what 😵💫
Nice Video, thank you.
Can you explain how i can highlight the button of the active page? Would be very helpful.
Thanks :)
I'm looking for tutorial like this for a while. I just want to ask you how can I build the same project using .Net Framework (Not .Net core)? and how to use MVVM inside my current project(I'm using .Net Framwork 4.6) ?...Thank you bro in advance.
Thanks. man!
Problem ist that this doesn't work in .Net Core for any reason (no problem in .Net Framework).
Edit: It DOES work in .Net when you switch the core version from 6.0 to 5.0 (and then back to 6.0). Maybe it's one of the Visual Studio's hiccups
can you make screen bigger, the font size is too small to read. any way good video
public void OnPropertyChanged([CallerMemberName] string propertyName = null){ ... }
can save your time typing the prop name
I like using CallerMemberName, but some projects I work in don't use it :( so I just always pass in the property name as a habit in case the project doesn't use CallerMemberName. Plus, I created a snippet that generates all the code for an OnPropertyChanged property, so it doesn't bug me much. For personal projects I should just use it though lol. Thanks for the suggestion Afterbunny!
Please try to make the fonts little bigger, very hard to see the text.
Hey DsaCpPractice, my videos have much better font size nowadays. This was actually my very first video!
Sir kindly make sure that you zoom in while delivering lecture rest is good.
This was my first video ever (as you can probably tell by the quality hahaha). My newer videos have much better quality. Thanks for watching, Daniyal!
It will be better if you share your source code also.
tyty
please increase the font size
Hey kumar, my newer videos have the increased font size. Check out this series if you want to see navigation videos with increased font size :) ruclips.net/p/PLA8ZIAm2I03ggP55JbLOrXl6puKw4rEb2
you should try to make videos when you are sober 😄
I'll consider it! 😄
Very complicated and much code only for changing content.
Why not making CunstomControls as Form and exchange them in DataContext on Mainform? That also needs no PropertyChanged for Mainform.
Hey mb, thanks for watching and glad to hear you enjoyed the video. This is WPF, so unfortunately I'm not sure what you mean by form. I also prefer keeping my navigation in the view model so I can navigate from inside a view model whenever I need to. However, I'm always interested in seeing other people's ideas in action, so it would be awesome if you linked to an implementation of your solution if possible.
@@SingletonSean This is what I hate about WPF, much code for less things.
But I must excuse myself, watching it on 2 times speed was too fast. You did it right and in correct way. Maybe you can save the ICommand by using code-behind with that click-event. But as always there are hundreds ways to solve a problem.
Also I never used a Property-Changed-Event for that, reassigning the data-context did an update by itself, if I remember right.
With "Mainform" I ment MainWindow., you also still used a UserControl, so please excuse my mistake.
But otherwise a very good tutorial and very good explained!
I myself experimented with a more crazy way, I made every model-view static and let them handle his Window by itself. Exchanging data between different view is easy that way, but it takes a little bit of that separation between UI and Code, but for most applications a correct done MVVM is way to much work to implement, so I tried a way between.
Hey mb, thanks for the feedback, I love discussing solutions and those are good points. I agree, could save an ICommand implementation or even use the "RelayCommand" that everyone seems to love, but like you said hundreds of ways to solve a problem, hahaha. Anyways, you're definitely right that it does take a lot of code to do some things in WPF, and making that choice really depends on the scope of your project. Like you suggested, many simple projects probably don't need to spend time implementing a pure MVVM solution. I'm sure you're not the only one who thinks this takes too much code, so I would like to revisit my implementation and see if I could simplify the solution. I've explored a lot with navigation since I made this video.
if this helps me, i will give an star on github 🙂
I am the like 1001
Easy xD
Easy WPF!
Terrible. I got lost within minutes even tho I tried super hard...
Hey Ironside, be sure to subscribe and check out my other videos for more help on getting started with WPF.