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
  • НаукаНаука

Комментарии • 80

  • @wmsl3944
    @wmsl3944 День назад

    Best WPF tutorial among I've found so far. Thank you so much.

  • @thomasmunguya2207
    @thomasmunguya2207 3 месяца назад +5

    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.👍

    • @KampaPlays
      @KampaPlays  2 месяца назад

      Thanks! I like that one too! C# has a lot of cool tricks like that (sometimes dangerous though!)

  • @thomasaminer
    @thomasaminer День назад

    Thank you. Now I understand how attributes work (I hope so)🙂

  • @manibaghdadi725
    @manibaghdadi725 2 месяца назад +3

    Literally the best wpf tutorial on the internet. I love it 🔥

  • @edinetgrunhed6000
    @edinetgrunhed6000 6 месяцев назад +2

    it explain more simple than the books I've read, more concise and direct to the point explaination

  • @wtcodingproductions
    @wtcodingproductions Месяц назад +1

    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 !! 🔥🔥🔥

    • @KampaPlays
      @KampaPlays  Месяц назад +1

      Thank you, really appreciate that!!

  • @qwerty3000
    @qwerty3000 6 месяцев назад +2

    Databindings used to confuse me, but you've just made it so simple to understand. Big ups, man!

    • @KampaPlays
      @KampaPlays  6 месяцев назад +1

      Awesome!! Glad to help!

  • @ebvs5ou
    @ebvs5ou 6 месяцев назад +1

    Absolutely great set of tutorials ! Very well structured. Thank you very much.
    Really usefull small tipps now and then like "propfull"

    • @KampaPlays
      @KampaPlays  5 месяцев назад

      Great, thank you, glad it's helpful!

  • @owenschaff292
    @owenschaff292 Год назад +6

    This series is great and databindings seem amazing, cant wait to see all the ways they can be used!

    • @KampaPlays
      @KampaPlays  Год назад

      Thanks! They are pretty awesome. Soon we'll do some MVVM and put them to use really well.

  • @wookyumkim4669
    @wookyumkim4669 Год назад +5

    10:48 Oh, I can't wait for the video about DataGrid 😂😂

    • @KampaPlays
      @KampaPlays  Год назад +1

      Won't be long from now, will be doing ListView as well!

  • @user-rd2dy4bs4h
    @user-rd2dy4bs4h 7 месяцев назад +1

    Ooooh, that is genius! Thank you very much for your amazing content!💖

    • @KampaPlays
      @KampaPlays  6 месяцев назад

      Thank you, glad to help!

  • @flaric590
    @flaric590 3 месяца назад +2

    You're tutorials are fire!🔥

  • @pierrestcergues
    @pierrestcergues 6 месяцев назад +1

    ouah! I had to watch it couple of times, but now it's crystal clear.. thanks!

    • @KampaPlays
      @KampaPlays  5 месяцев назад

      Awesome! Glad it made sense!

  • @RaqiGirl
    @RaqiGirl 3 месяца назад +1

    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! 😁

    • @KampaPlays
      @KampaPlays  3 месяца назад

      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!

  • @user-rd2dy4bs4h
    @user-rd2dy4bs4h 7 месяцев назад +1

    The topic seemed pretty complex, but I was able to understand it, thanks to your brilliant explanation!

    • @KampaPlays
      @KampaPlays  6 месяцев назад

      Really appreciate that!!!

  • @wmsl3944
    @wmsl3944 День назад

    Ты очень классно рассказываешь,🤟

  • @SpeedSharp23
    @SpeedSharp23 11 месяцев назад +6

    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.

    • @KampaPlays
      @KampaPlays  11 месяцев назад +2

      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.

    • @SpeedSharp23
      @SpeedSharp23 11 месяцев назад +1

      @@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 😊.

    • @KampaPlays
      @KampaPlays  11 месяцев назад +2

      @@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!

  • @yadmhedin972
    @yadmhedin972 2 месяца назад +1

    the best tutorial love you man

  • @FionaldyAManik
    @FionaldyAManik 6 месяцев назад +1

    BROOOO YOU'RE SO SMART, THIS IS SUPER HELPFUL!!!!!!!!!!!

    • @KampaPlays
      @KampaPlays  6 месяцев назад

      Glad it's helpful, thank you!!

  • @DharmikGohil-bw2ug
    @DharmikGohil-bw2ug 6 месяцев назад +1

    Really Great explanation Thanks 👏

  • @unkxxwn_name
    @unkxxwn_name Год назад +1

    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

    • @KampaPlays
      @KampaPlays  Год назад

      Haha! That is awesome!! Everything making sense and sticking with you?

  • @azadalmohammad-rm7we
    @azadalmohammad-rm7we Год назад +1

    Keep up the Amazing Work!!

  • @mjmo6919
    @mjmo6919 Год назад +1

    Very nice tutorial

  • @nguyenmanh6751
    @nguyenmanh6751 8 месяцев назад +1

    thanks you very much

  • @furiouslobster7912
    @furiouslobster7912 Год назад +2

    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

    • @KampaPlays
      @KampaPlays  Год назад

      Yes, I'm saying "GUI", sorry for the confusion!

  • @priyankaabbineni4231
    @priyankaabbineni4231 Год назад +2

    Could you please do a video on wpf treeview controls in MVVM pattern and how to add data dynamically to treeview

    • @KampaPlays
      @KampaPlays  Год назад

      Will add this to my list! thanks!

  • @MRGM26
    @MRGM26 5 дней назад

    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?

  • @wookyumkim4669
    @wookyumkim4669 Год назад +2

    8:30 😆👏👏👏👏👏👏👏👏

  • @jonathanmoore5619
    @jonathanmoore5619 Месяц назад +1

    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?

    • @KampaPlays
      @KampaPlays  Месяц назад

      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.

    • @jonathanmoore5619
      @jonathanmoore5619 Месяц назад

      @@KampaPlays thank you.

  • @Onyx31415
    @Onyx31415 Год назад +4

    Hi, I really love the series so far.
    Why, do you create a public and private property for BoundText?

    • @KampaPlays
      @KampaPlays  Год назад +5

      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?

    • @Onyx31415
      @Onyx31415 Год назад +3

      @@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

    • @KampaPlays
      @KampaPlays  Год назад +2

      No problem, great question!

  • @AhmadAnwar31Aan
    @AhmadAnwar31Aan Год назад +1

    👍👍👏❤

  • @standman007
    @standman007 Месяц назад +1

    100 likes for this tutorial. Thanks

  • @vitalijgrabovan2061
    @vitalijgrabovan2061 Год назад

    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.

    • @KampaPlays
      @KampaPlays  Год назад +1

      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)

    • @vitalijgrabovan2061
      @vitalijgrabovan2061 Год назад +1

      @@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.

    • @vitalijgrabovan2061
      @vitalijgrabovan2061 Год назад +1

      @@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 !!!

    • @KampaPlays
      @KampaPlays  Год назад

      Awesome!!! Thanks for letting me know!

  • @nubdevice4412
    @nubdevice4412 11 месяцев назад

    Umm what? Screw placeholder?
    Fine I accept defeat. Guess I'm going to just set it like I'm told not to.

    • @KampaPlays
      @KampaPlays  11 месяцев назад

      Sorry I don't understand, which part are you referring to?

    • @nubdevice4412
      @nubdevice4412 11 месяцев назад

      @@KampaPlays it's okay. I was a little disappointed this episode didn't apply well with the previous. Sucks being dumb.

    • @KampaPlays
      @KampaPlays  11 месяцев назад +1

      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!!

  • @tsunamio7750
    @tsunamio7750 5 месяцев назад

    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.

    • @KampaPlays
      @KampaPlays  5 месяцев назад

      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.

    • @tsunamio7750
      @tsunamio7750 5 месяцев назад

      @@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

  • @junulee3584
    @junulee3584 Год назад +7

    Best WPF tutorial among I've found so far. Thank you so much.

    • @KampaPlays
      @KampaPlays  Год назад

      Great, thank you!!!

    • @dzendys_
      @dzendys_ 6 месяцев назад

      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