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: ru-vid.com/group/PLA8ZIAm2I03ggP55JbLOrXl6puKw4rEb2
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.
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!
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!
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.
@@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...
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.
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?
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?
@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
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 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.
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?
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.
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
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?
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.
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
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
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 😵💫
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
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?
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
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?
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?
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.
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
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! ru-vid.com/group/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 😤
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
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.
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.
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.
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.
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 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.
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 :)
@@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?
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 :)
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 :) ru-vid.com/group/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.
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.
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?