C# WPF Tutorial #24 - Using RelayCommand in MVVM
HTML-код
- Опубликовано: 6 июн 2024
- Continuing to implement the MVVM pattern, adding RelayCommands so that we an bind our button commands to actions in our view models!
Happy Coding!
Chapters:
0:00 Setup
0:37 Button Command Binding
1:00 Implementing ICommand (RelayCommand)
4:25 Using RelayCommand
6:00 Using Predicates (CanExecute)
8:40 Recap & Tips
9:20 Next Up - Наука
Please!!! dont stop with this series. I can learn a lot about WPF with your videos.
Thanks so much! I will be back at it soon, still playing a bit of catch up at work.
@@KampaPlays Nice to hear! :)
yes please
@@KampaPlays Thanks for the videos! I haven't coded in C#/.NET since 2010 and I need to make a tool to work with multiple json files....
Not only has the refresher been good, but a lot has changed since then.
I have to build an app that uses 39 json "loot table" files that are all different and create a UI that allows users to edit the files in an understandable manner, then save them again so they can be used by a multiplayer game.
Not only do I not have any control over the file format, none of the files contain a complete listing of everything that _can_ exist in them. Since a person can use the tool/app to completely keep an item from spawning in-game (a specifically requested feature) would prevent them from adding it back, one of the tasks I have is figuring out how to combine the "default" files into a master 'database'. I _also_ have to collect the various values of the properties in the files as those could be lost as well.
So not only do I need to figure out how to build a single reader that can interpret all the files (that will also hopefully work with future versions of said files), I need to implement a configuration file and a local data file that contains all the possible things, places, and categories (all just different values of 'Name'), rarity values, etc.
LOL, then I have to be able to spit it all back out into 39 modified .json files and I don't even know if making structural changes to the files will keep them from working. I know I can add/remove certain levels of child nodes, but the position of those specific child nodes changes from file to file...
You have no idea how much you saved me. I applied for a JavaScript job, but they recently wanted me to make a Windows application using WPF. It's very different. I had no idea about MVVM and data bindings. Your videos helped me a lot with building the base of knowledge in WPF, and even after 5 months, I still come here to relearn some stuff because you are the only one who explains things well, at least for me. Your videos are mostly basic stuff, but VERY essential. I appreciate all your work in making these videos. Greetings from Chile.
Thank you so much for this series. Many people around the world is watching this series even now.
Thank you!
Extremely helpful series. Thank you so much for putting these on RUclips. Like others on here, I hope to see this series continue.
Thanks so much. Hope to continue soon!
@@KampaPlays Lets go!
Nothing but praise to this amazing series! I really hope work/life slows down a bit so you can continue these awesome tutorials.
Yes, absolutely 💯
Thanks so much. I'm thinking (and hoping) that I will have enough time to really get back at it by late spring.
Honestly the best tutorial on WPF so far! You rock!
Thanks so much!
I agree, good and short videos, better than some very long videos
Very simple up to the point video that is suitable for me as a beginner thank you
Glad to help!
It would be great if you contiued this series. I'm an old school coder starting to learn these new fangled ways of doing things. Your tutorials have helped!
Hopefully soon, very glad to have helped!
i just finished watching this video. can't wait to watch the next video. please continue making this series i've learned a lot from this series and i thank you very much for it.
Thanks so much! Will do.
I have been loving these tutorials!
Awesome, thank you!
The best WPF tutorial out there!!!! A huge thank you for creating this! As a self learner I'm really looking forward to the next episodes. 😃
Thank you!
man this tutorial is crazy good, you just got a subscriber, Please update the course weekly
Thanks so much! I try to do one/week, but lately and for probably a couple more months, work is slowing me down a bit, but I'm still at it!
Just started using WPF and this is by far the best tutorial ive found. Subbed for future stuff.
Thanks so much!!
"Wow, your tutorial videos are absolutely amazing! The best one I can say from all the turorials I have checked. I can see your passion for teaching. Even with the small details. Your tutorials have been instrumental in helping me as a starter. Thank you for sharing your knowledge. Keep up the fantastic work!"
Thanks so much, I really appreciate it and am glad they have helped!
Thank you for these amazingly good tutorials. The best I've found. I do hope you will keep doing them.
Thanks so much, I'm hoping to be able to have time to get back to them by late spring.
I enjoyed watching this little crash course, the topics you selected are covering everything needed for a starter project. Thank you for your time you have been putting into this series.
Awesome, thanks so much!
Can't wait to see more. Please don't stop publishing and if you have more bring it on the table!!! :-)
Just wanted to say, these videos are fantastic! Can't wait for the next one!
Thanks so much, will be jumping back in soon!
This tutorial really encouraged me to learn more on WPF! You are doing great job, waiting for the upcoming series!
That's great, thank you!
So glad I came across your channel...
Haven't understood C# and Wpf so easily as I do now...
Will surely be waiting for more videos
Thanks a lot
Thank you. Very glad to help!!
Loved your series :), it's the best WPF series i've found on YT
Thanks so much!!!
I learned so much from this series that I can now apply to my homework :D Thanks man, keep it coming!!
Awesome!! Thanks!
BEST TUTORIAL EVER on WPF !!!! I have a subscription on LinkedIn Learning, but not even they could teach me as much as you did. Thanks a lot!
Awesome, thank you so much!
Thank you a lot for making this WPF tutorial, very concise and helpful! Waiting for the next video :)
Thanks so much. Hopefully I'll be back at it soon!
3 plurasights courses just to understand what the commands are good for, took you 6 min to explain. Well done and thanks!
Thanks, glad to help!
I am waiting for your next youtube series about WPF. It was so good time to step here with you. I am touched. Thanks a lot~~!
Thank you!!
Wow, you provide verys simple explanation to seemingly complex topics, Thanks
Really appreciate that!!
love it, every word you say actually has meaning to it
Thanks so much!!
Your tutorials are amazing! Hopefully your work slows down so you can keep these up because they have helped me immensely!
Thank you!
Excellent. Exactly what I was looking for.
Great!!
Hi! Software engeneer in my final semester here!
I just finised your series, and i must thank you, because it really helped me a lot. I had a lot of stress towards coding because i found it challanging and scary after my past experiences with it, and i was at a point where it easily gave me panic attacks to open a Visual Studio. I really thought that Software engeneering is not for me, but was scared to change majors this late. I not only learned from your series, but with your style of teaching, i got used to concepts and functions in coding, and i am proud and thankfull to say that i no longer have fear towards programming, i just need to learn more about it, and get familiar with it. This is all thanks to you, and how you introduce everything in this series, in a way thats understandable, short, and yet helps me to see how it would work in real life. If you continue this series, i'll keep following it, since it's the best one i have ever seen (And belive me, i watched a lot of 2+hour tutorials). But if not, for whatever reason, i still want you to know that your efforts in these videos saved my future carrier. Thank you!
This is amazing. Thank you for writing, it means a lot.
Great job powering through, it will pay off!! It's easy to get overwhelmed in this world, even for people doing it for many years. Just remember that the computer does only what you tell it to do, and take it one line at a time! :)
Work(s) have been busy but I will be continuing as soon as I can.
I wish you the best; thanks again!
Hi @kampaplays , thank you very much for this detailed and to the point tutorial. I have no previous knowledge in wpf and I had an interview in coming week. I am following your tutorial to be prepared for the interview and its very helpful.
Great job getting prepared, I hope your interview goes well!
If you think about, let me know how it goes!
You are amazing and i love your tutorials from Egypt
Thank so much!!!
The content is really good and how you explained everything is so clear and understandable. I felt that you have covered most of the stuff needed for a beginner in WPF. So far the best WPF tutorial I've seen. I have a request, can you please make a video on using Telerik in WPF applications.
Appreciate that! Do you have any specific wishes for Telerik controls or just how to incorporate the library, etc?
Ty. I love your videos
Thanks so much, I'm very glad its helpful! Will be back at it soon.
Very nice video as always👍🏻👍🏻. I have learned a lot of things with the help of your videos. My dream was always to be a good programmer. With your help I hope I will achieve my purpose.
Thank you!! I'm glad they are helpful. Keep at it, the best way to be a coder is to write code!! 😁
I started my internship 4 weeks ago and got to work with WPF the first time there. Not much was explained and it looked simple enough to work with. It went well until I got stuck on a problem and no matter what I looked at, I didn't get what i needed. So I decided to watch your videos which made me understand how WPF actually works.
I fixed my code a bit and customized it a little cosmeticly.
Just can't get the styles to work for some reason which I would have liked to get rid of some redundant code.
However, I still didn't manage to solve an issue.
What my program does right now is that it has 3 textboxes, a button and a datagrid. 3 are reading the input and 1 of the text boxes uses 2 inputs to calculate something using those values and show it on the one of the textboxes. Everytime I change the parameters and click the button, it shows a new value.
Now I want the Datagrid to do the same but instead of just showing 1 solution, it suppose to show 25 different solutions. I want to create a table that gets generated whenever the button is pressed just like the 3rd textbox does and change whenever I change parameters and click the button. First column suppose to show the numbers 1 to 25 and the second column calculate using the parameters from the first 2 textboxes times the value of the first column that is in the same row.
That is the major thing I am stuck right now.
You will likely need to bind some ObservableCollection type to your dataGrid and from there create and populate your data so it displays correctly. I don't have anything specific to that yet - but this may be helpful if you are still having issues. (sorry for my delayed response)
stackoverflow.com/questions/24539022/how-to-bind-wpf-datagrid-to-observablecollection
Congratulations Excellent tutorial. 🔥🔥🔥🔥🔥🔥🔥🔥🔥
Thank you!!!
Thanks for your videos!
Glad to help, thank you for watching!
Wow, great series
I'm in my 50s now, and I've been working with Excel for years (as a "hobbyist"). Someone where I work created an ordering form (in Excel). Its structure and performance really offended me, so I had to fix it😃. And I did (from home) problem was I had a newer version then they had at work and I didn't want to do the crazy (unnecessary) work to make my form work in their older version.
I've played with C# in the past (for an Arduino project) and had a lot of fun doing it. So, I started on a quest to see what I could do with that. Then I found you and WPF 😁. True, I don't fully understand everything, but I am getting the hang of things a little at a time 😄.
I really enjoy coding but I think I'm a little to old now to be jumping careers 😪(this stuff is crazy intense 😮 sometimes) but I thank you for making your videos it's been fun trying to" play with something new (at least new to me). I can't wait to continue working on different/new projects with you
Thank you, and I wish you well 😃
Excellent!! You sound like a software engineer to me, especially getting offended at structure and performance! 🤣
Keep at it, and even if you don't want to leave your current job, there's tons of remote/freelance gigs out there if you want to turn your tinkering into dollars!
Great series I need a refresher for my notes Thank you!
-Jobson
Thanks so much, glad it helped!
I have really enjoyed your C# WPF Tutorials. I feel like I've learned a great deal from them. I am wondering if you have, or can recommend, tutorials for accessing SQL databases? Thanx
That was quick! So glad you enjoyed them. I don't have any video tutorial references on hand - I really want to do a series on databases when I have more time.
Browsing around, I found this, it looks like it covers the important no-frills howtos of C# + MSSQLServer: zetcode.com/csharp/sqlserver/
I think learning this is most important, rather than starting with an ORM like entity framework that obfuscates how things happen. Many times a simple database application, especially a personal one, is easier to just implement yourself rather than take on the extra complexity.
From there, if you are interested, you might want to check out additional tools/ORMs like:
Dapper: www.learndapper.com/#what-does-dapper-do
Entity Framework: learn.microsoft.com/en-us/visualstudio/data-tools/create-a-simple-data-application-with-wpf-and-entity-framework-6?view=vs-2022
If you are looking for a lighter-weight database option, SQLite [flat file, non-server] may interest you. sqldocs.org/sqlite/c-sharp-sqlite/
Can't wait for the next one!!!!
Thank you, hopefully I should be back at it soon!
Waiting For Multithreading....Please continue doing video's...
Excellent tutorial
Thanks!
Hey, I'm so new to this channel, just needed some help in general with wpf, since im trying to develop a linrary for custom controls 😅...
Thanks for all the help ... But gimme more 😂❤
Good luck on the library!! Hopefully I'll be starting back up soon, too.
the best tutorial!!!
you deserve more subs, keep up the good work!!!
Thanks so much!!!
new subscriber here
Thank you!!
It's great u point small things as well which creates confusions, keep it up like this...
When is the next video eagerly waiting.
Hope Coming Soon ends Soon.
Thank you! I hope to get back to it in the next couple of weeks or so once work calms down.
congratz. good job
Thanks!
Interesting video like the previous ones. Can you make a video on how to navigate between pages in wpf?
It is on my to-do list! thanks!
awesome !!
Thanks!!
The coming soon next episode is wanted! :-)
Wow plz don't stop this series specially WPF MVVM, can you make a content that create a project using C# WPF MVVM pattern?
I'll be continuing the series, I will try to start from the ground up to create a whole project.
@@KampaPlays thank you so much, well appreciated
Legend thanks
Great tutorials. Are you planning on making a video on how to navigate between pages/UserControls/Windows and its differences using MVVM and Commands anytime in the near future?
Would really appreciate someone to explain this in another way than hour long non-commmented recordings :-)
Thanks, yes navigation and swapping between views/viewmodels are both on my todo list!
@@KampaPlays will be waiting
Hey @KampaPlays thank you so much for this tutorial. I hope you can get back to it because I saw in the comments that it is helping so much people around the globe. I already saw many tutorials on RUclips and Udemy and I could not find anyone as good as yours. Congratulations for that.
One suggestion that I have, will you continue with MVVM showing samples about passing data from one page to another page? and if you get back to that previous page, the data inserted remain? I could not find any tutorial explaining that yet on the internet and it will help me so much.
Last question, I noticed trhat you always create WPF Windows and most people create from User Control. Whats the difference?
Thank you so much master!
Sorry for the delay.. really appreciate the thoughts!
I will definitely make a deeper look with navigation and data passing/persistence. Many times it can be as easy as maintaining references to your active views' view models, and the data will be held in those objects, but it's close up on my todo list when i can get back into it.
Usually for these tutorials I just create a single Window and put logic into it. Window comes with behaviors to start/close the application as well as minimize/maximize/etc. User Control is basically UI space with nothing extra, so its great for separating out pieces, creating reusable widgets, swapping panels, etc.
Thanks
Thank you, really appreciate that!!
Nice tutorial series, I learned a lot. I am a beginner in WPF o maybe my question is basic: do you plan to make tutorial about how to add and remove elements/uer controls from grid or other elements like stackpanel? I thinnking about like miniatures appear in the windows explorer.
Thank you, I'm glad it was helpful. I can add this to my todo list!
Great. Where Can I find the sources of this playlist?
thanks for sharing, can i ask a question, in MVVM where i can call API or logic
You would call API/logic inside of the ViewModel itself. That way you can use the results/response to update your UI and your models. You may want to separate the bulk of that logic into another class to be called from the ViewModel if it makes sense to do so.
very nice video playlist
is there any chance for more videos for WPF?😀
Thank you, hopefully soon!
Great video as always. If there aren't any major downsides to it, I think I'll just go with implementing each command as a separate class since I have absolutely no clue how lambda expressions work or whatever that was you used...
Thanks!
I would strongly recommend using the generic version of the RelayCommand class. It will keep you from ending up with hundreds of lines of duplicated code, and if changes ever need to be made, they can be done in a single place. Expression bodied members are a bit crazy looking at first, but they are an amazing tool in C# I and definitely think it's worth learning!
learn.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/expression-bodied-members
@@KampaPlays That's interesting because I saw this video: ruclips.net/video/bYPU8X8f2xU/видео.html (this is his conclusion) from a seemingly experienced guy, who is also making wpf tutorials. He talks about the pros and cons of creating a class for each command and he also presents an example in which using relay commands would actually result in duplicated code. So I got the impression that it doesn't really matter which way to do it... I'll definitely still look into expression bodied members though!
@@dariostromajer1625 Hey, that was a good watch, and a pretty clean way to do things. I wasn't trying to say it was wrong, but mostly that I would give it a go (especially with expression bodied members themselves) before you go with the other method. I've used both methods with success, but what I've noticed is with a team of people, things can get much muddier faster with command specific implementations, but as long as you follow a good standard and keep consistent I say do what you are comfortable with.
Does this also work for events?
could you do a tutorial on how can two user controls communicate between each other and dynamically replacing parts of the ui based on what is say selected in a tree view or datagrid
It's so sad that Kampa Plays did not make the multi-threading video. I would use that video.
Any suggestions on books or other resources to continue to learn WPF?
My biggest suggestion would be to pick a personal/work project you want to complete and let necessity drive your learning using the Microsoft available resources. learn.microsoft.com/en-us/dotnet/desktop/wpf/controls is a good place to start, they have lots of information for each component including sample code and examples.
Hey do you know
How can we get wpf in macOS
It is not showing wpf in it
I want to know is there anything specific I have to do or there is alternative
I am really into it and I have MacBook
As far as I know, WPF is strictly Windows. There are several WPF-like alternatives that are cross platform though. Check out Avalonia UI. MAUI also is similar, in that it uses XAML, but Avalonia is much closer to native WPF.
Great tutorial. Can you make a video for mvvm community toolkit as well?
Thank you. I'll add this to my list, but it may be some time before I get to covering any specific 3rd party packages.
Great tutorial! but I just want to know, if Is it possible and if so, how can I change the SelectedItem to SelectedItems so I could remove multiple rows at once. 😁
Thank you!! It is possible, but since you can't bind directly to SelectedItems, you have to use another way such as a CommandParameter or custom control. I will add this to my to-dos!
This might help clear things up in the meantime: stackoverflow.com/questions/9880589/bind-to-selecteditems-from-datagrid-or-listbox-in-mvvm
Every other video on the WPF playlist was crystal clear. This one was very confusing. Maybe it will be clearer if I look into what commands are in WPF controls.
I understand expresion-bodied members, but don't understand the purpose of providing Action and Func to the Interface's methods.
Sorry for the confusion -- WPF bindings and commands are one of the more "black magic" things that are hard to see how they work.
Those parameters are provided so you can make RelayCommand more generic. You can pass any function to serve as your command action, and any function to serve as your predicate (whether or not you can do that action).
So for example, using a button, you can bind a RelayCommand, and provide a "DoThis()" and a "CanDoThis()", and it will handle things like disabling the button based on "CanDoThis()" to running "DoThis()" when the button is clicked.
I hope this is helpful.
@@KampaPlays yes I think I understand. What's the alternative? Do you create a new class with new methods for each button?
If I have a method that takes a parameter to perform a task based on the parameter, how would I include a CommandParameter to the command?
Thanks for the great tutorial. It helps me a lot.
Are you asking how you can pass something to the Execute method of your command? If so, you can set the property CommandParameter on your button in xaml. You can also do it in code-behind with yourButtonName.CommandParameter = something...
Thank you! Dario's answer is correct, and I will add these to my todo list, as it can be confusing!
please make more!
Will be back at it as soon as work calms down a bit!!
please continue...
Amazing tutorial!!!
But can anyone tell me if / how i can pass augments (CommandParameter or simillar) to the Relay Command?
Got it :D
In Viewmodel:
public ICommand delIng { get { return new RelayCommand(param => deleteIng(param)); } }
void deleteIng(object ing)
{
//Do sth
}
Hello! Watched already all your videos about WPF and want to say thank you for the job you've done!
Although in the end of the video you're telling that a video about threading is coming soon but this is the last video. What happened? :(
Appreciate it. Yeaaaah, life happened. Lots of work and family things. I am planning to return as soon as I can with a continuation of the series and advanced topics, but I have to get past a few more hurdles first! Thanks for watching :)
@@KampaPlays damn, hang in there!
Can you make a tutorial for communicating between two viewmodels, create an n-tier solution structure and work with EF. Thanks a lot!
Will add it to my todo!
@@KampaPlays Thanks a lot.
More vedios on mvvm
Can you make a tutorial on accessing data with EF Core on WPF ?
Will add to my list of to-dos.
Please teach how to handle different screen resolutions
Many things in WPF "handle themselves" when it comes to resolution changes -- What are you interested in doing?
Please make a tutorial on inkcanvas with infinite drag/scroll. Thanks 🙏
Will add to my todos!
@@KampaPlays please do
Can you pls do video on tree view dynamic binding pls…
This is on my todo list, thanks!
Where is the code? If I am not finding any code with this video, it's a waste of time to follow your video.
I don't include code with these short, beginner level tutorials. This is to help the most people learn and retain the information. The best way to learn to code is to write code.
@@KampaPlays I have thirty years experience and just switched to WPF. I’d encourage you to include the code because people don’t have time to write code for these. I’m finding solutions elsewhere and now I look for code before viewing.
@@talktodale I appreciate the feedback and definitely understand your point (I don't watch tutorial videos myself, as its faster for me to just reference the documentation).
I will absolutely include repos for more complex/lengthy topics, but my primary goal here is to help people learn, understand, and retain. I would highly recommend going straight to Microsoft
learn.microsoft.com/en-us/dotnet/desktop/wpf/
and skip videos entirely if you are an experienced programmer, as it has a wealth of example code for everything you might need. Again, thanks for the feedback and best of luck to you.
Can It get a job in cina?
With remote programming jobs being so available, I bet you could get work anywhere! :)
Hi Kampa, may i contact you per email?
Sure -- kampaplays@gmail.com but honestly I don't check the mail as often as I check comments here!
I d like to get professional support from you and i believe it wont take too much time from you. I already have an approach (basic solution) and would like to discuss you whether my solution is optional or acceptable. (I have a set of historical collection and i d like to filter based on filter text box (a string prompted by water watermark) i convert this string into column name and value pair and then filter this column by that value given by filter text box(ps. Orc filter control is used from wildgums)