C# WPF Tutorial #8 - Data Bindings using INotifyPropertyChanged
HTML-код
- Опубликовано: 7 июн 2024
- Learn how to logically separate your GUI and business logic by using data bindings and the interface INotifyPropertyChanged. This allows us to update our GUI from our code as well as populate our code properties from the GUI without having to reference our GUI elements directly. This supports good coding and testing practices as well as making your life much easier as your projects grow in size and complexity!
Happy Coding!
Chapters:
0:00 Intro
0:19 Setup
0:38 Creating our Property
1:47 Setting the Data Context
2:33 Implementing INotifyPropertyChanged
3:58 Invoking PropertyChanged
5:06 Binding UpdateSourceTrigger
6:34 Updating the GUI via Property
7:42 Binding Mode
8:52 Refactor for Reuse
10:36 Additional Info
11:01 Next Up - Наука
Best WPF tutorial among I've found so far. Thank you so much.
I love how you not only show what to do but also the best way to do it. There are so many tutorials out there which just show how to do something without taking into consideration the best way to do it, or let me say without taking into consideration best practices. The best part for me in this tutorial is how you made sure that we are reusing code and taking maintainability into consideration by making the OnPropertyChanged method get the calling member name by itself.😅 By the way, I just learned about the CallingMemberNameAttribute right from this tutorial. Keep it up.👍
Thanks! I like that one too! C# has a lot of cool tricks like that (sometimes dangerous though!)
Thank you. Now I understand how attributes work (I hope so)🙂
Literally the best wpf tutorial on the internet. I love it 🔥
Thanks so much!
it explain more simple than the books I've read, more concise and direct to the point explaination
Thanks so much!
A literal, genius and expert on code. Definitely understands what's going on 'prior to' and during 'runtime' ... No cap given. And, I've watched a lot of C++/C# tutorials prior to this...
"The instruction is truly ABOVE par here."
Thank you, Brother! @Kampa Plays ...Let's GO ! VIBES !! 🔥🔥🔥
Thank you, really appreciate that!!
Databindings used to confuse me, but you've just made it so simple to understand. Big ups, man!
Awesome!! Glad to help!
Absolutely great set of tutorials ! Very well structured. Thank you very much.
Really usefull small tipps now and then like "propfull"
Great, thank you, glad it's helpful!
This series is great and databindings seem amazing, cant wait to see all the ways they can be used!
Thanks! They are pretty awesome. Soon we'll do some MVVM and put them to use really well.
10:48 Oh, I can't wait for the video about DataGrid 😂😂
Won't be long from now, will be doing ListView as well!
Ooooh, that is genius! Thank you very much for your amazing content!💖
Thank you, glad to help!
You're tutorials are fire!🔥
Thank you!!
ouah! I had to watch it couple of times, but now it's crystal clear.. thanks!
Awesome! Glad it made sense!
Here I am again. Thirty-five years ago I went to college to learn all I could about programming. We were just starting to talk about object-oriented programming at the time and trying to learn the basics. Thirteen years ago I retired and went on to other things, such as traveling N. America in an RV. Recently I've wanted to write some code to watch our investments while we play here in Panama and at seventy-five have decided to "dust off the brain" and start learning a current, object-oriented language. After a few months of looking around I liked what I saw in C# and have begun learning. After looking at some other teachers and their tutorials I liked what I saw with @KampaPlays. I just hope now that I've found a teacher that you are able to keep up with me! 😁
This is so great. It sounds like you are living the most out life! C# is a great OO language. It is extremely versatile, and while it has great high level features like garbage collection, it can also get about as low level as you want it to, without getting into assembly. Happy coding!
The topic seemed pretty complex, but I was able to understand it, thanks to your brilliant explanation!
Really appreciate that!!!
Ты очень классно рассказываешь,🤟
Hi there. I've been learning how to code in c# for about 5 months now and decided I'd like to try adding GUI to my console applications. I found this playlist and I have to say...This is great. Simple to understand and easy to follow. However, there's one thing I just can't seem to understand.
By using INotifyPropertyChanged and implementing PropertyChanged event, and then invoking it inside the setter of property, we can notify GUI about property change.
What I don't understand is, if I'm not mistaken, when we invoke the event, event handler methods that are subscribed to the invoked event are supposed to start and they are supposed to do the logic part.
But if I'm looking at the code right, you never created an event handler method that subscribes to the PropertyChanged event, so PropertyChanged event value should be null/nothing should happen, and yet PropertyChanged?.Invoke(...) worked.
Am I missing something, or are event handler methods for PropertyChanged event already built-in, similarly to how events like Click seem to have built-in event invoking method?
In other words, I'm trying to understand why PropertyChanged != null; at invoke time.
Sorry for this long of a message. Hopefully, you understand what I'm asking, since english is not my first language.
Great question, and you are right. INotifyPropertyChanged and the PropertyChanged event does work a bit differently than a user-defined event, in that it is already "hooked up" for you for any compatible UI elements in your data context. All you need to do is implement the interface, and invoke the event telling it which control to notify, and the rest is done in the background.
@@KampaPlays Nice. Shortcuts like these really make programming easier, but they also tend to confuse me when I'm trying to learn something 😅. In any case, thanks for your help 😊.
@@SpeedSharp23 Totally understand that. While I'm used to most of the "black magic" behind-the-scenes WPFisms, I prefer to see the logic myself!
Glad to help!
the best tutorial love you man
Thanks! Much love!
BROOOO YOU'RE SO SMART, THIS IS SUPER HELPFUL!!!!!!!!!!!
Glad it's helpful, thank you!!
Really Great explanation Thanks 👏
Thank you!
I think I might got lost somewhere, its 3 AM, just yesterday I learned how to print Hello world on the console and now I am here
Haha! That is awesome!! Everything making sense and sticking with you?
Keep up the Amazing Work!!
Thank you!!
Very nice tutorial
Thank you!!!
thanks you very much
Welcome!!!
Hey, you've mentioned several times "gooe"(?). Do you mean GUI or is it something else. English isn't my native language so it got me quite confused lol
Yes, I'm saying "GUI", sorry for the confusion!
Could you please do a video on wpf treeview controls in MVVM pattern and how to add data dynamically to treeview
Will add this to my list! thanks!
getting error
Error (active) CS0103 The name 'InitializeComponent' does not exist in the current context
How to fix this this basically i have created custom component and all file showing this problem?
8:30 😆👏👏👏👏👏👏👏👏
Great video. Can i just ask DataContext = this....
Does that mean that the data associated with this wpf project.. is set and written within MainWindow? And could be set to another class, if you want? And what about if you have data in main window and an external or within project namespace class? How do you do that?
Yes, and yes! Generally when you do this you create a "ViewModel" and assign the data context to that, separating your UI from your logic (very similar to controller in MVC). Tutorial #23 goes over the basics of creating and using one.
@@KampaPlays thank you.
Hi, I really love the series so far.
Why, do you create a public and private property for BoundText?
Thanks!!
The public property requires a private backing variable to store the data. The property really only acts as a "getter" and "setter" of that private variable. Even if you create an auto-property (e.g. public string Text {get;set;}), the compiler will create this private backing variable for you. Here we need a full property, mainly so that we can define our PropertyChanged behavior in the property's set method. Does that answer your question?
@@KampaPlays Ah, I'm a bit new to c# and didn't know it worked that way behind the scenes. Thanks a lot for your answer
No problem, great question!
👍👍👏❤
🥳
100 likes for this tutorial. Thanks
Thank you!!
Hello Krampa, I have one issue and it seems that I can’t find the answer on it, so I hope that you might help :)
In my MainWindow I have a radio button with a XAML tag="*" in my MainWindow I can see that this tag has "*" as a parameter.
The problem is that when I want to pass this Radio Button with that tag to my different class "MyClass" the tag name is empty="null" why it’s not passing?
What do I need to do to pass the parameters from MainWindow to MyClass, is it done by Data Binding?
Thank you.
It depends on the purpose of your other class. You might want to check out tutorial #23, It goes over ViewModels, which use databindings to get data from the UI to another class automatically -- that may be what you are needing.
If a ViewModel-type interaction is not what you are after, I would recommend creating some state variable or object to represent your radio button(s) states and any other UI states, and do not pass the actual UI controls anywhere. For example, when your radio button state is changed (or other event):
string radioTag = yourRadioControl.Tag;
otherClass.Update(radioTag)
@@KampaPlays Thanks I will try it :) I have solved this issue by adding a constrution in my class that i need to get a radio button Tag in and passed that value to it. Works quite good but I think its not the best solution.
@@KampaPlays So I did watch your 23th video for several time to understand all of it and at the end I have solved my problem :) thanks your the best !!!
Awesome!!! Thanks for letting me know!
Umm what? Screw placeholder?
Fine I accept defeat. Guess I'm going to just set it like I'm told not to.
Sorry I don't understand, which part are you referring to?
@@KampaPlays it's okay. I was a little disappointed this episode didn't apply well with the previous. Sucks being dumb.
Ha! I bet you are not dumb! This part of WPF can be really hard to get because so much of it happens like "black magic" behind the scenes. I tried breaking it up to best understand what is happening, but maybe it just wasn't how you needed to see it.
So I think I understand now, but if not let me know. The previous video we set the control's property directly, which was a placeholder instead of implementing "OnPropertyChanged". I wanted the last videos focus to be on how to make a custom control and not pile on the bindings portion. So in this video, it shows how to implement the interface INotifyPropertyChanged so that you do not have to set the control's property directly, but instead call "OnPropertyChanged" to let it update via binding.
I hope this helps!!
10:18 Have you ever heard of side-effect-programming?
Please, end my suffering.
I mean, WPF is easy... you just have to learn everything, and it does not apply to any other language or any other tool.
This is pure horridness. Death shall feel good once I'm off this acursed playground of slow, bloated corporate shit.
Ha! Yes, there is a lot of behind-the-scenes 'black magic' that WPF (and so many other UI frameworks) does that I'm not a fan of. The suffering is extra real when you are forced to use strategies and patterns when they aren't necessary. They can be very useful in the right context, but if reasonable, save the separation of concerns worries and give me my visible, readable, and readily traceable code all day.
I hope your suffering wanes, best of luck out there.
@@KampaPlays Hey, I just made a whole Kanban UI. I'm delving into the MvvM. It's not so bad.
I can't thank you enough. Once I get back to work, First salary, first tip for you
Best WPF tutorial among I've found so far. Thank you so much.
Great, thank you!!!
I agreee, I was totally lost, when i saw OnPropertyChanged() in someone's other tutorial.This video has helped me to finally understand it well and how it works in the background. Not the classical: "okay, so this does that and when does this happen, this will be called and this will happen". Thanks