C# WPF Tutorial - Using INotifyPropertyChanged

  Рет қаралды 48,790

ToskersCorner

ToskersCorner

Күн бұрын

In this tutorial we will learn how to implement the INotifyPropertyChanged in our WPF applications to update property values to be displayed on our views when they are changed. In this episode we create a base 'ObservableObject' class to make this feature universal for all of our view models. We use this to change the background color of our application and to change a name value for a welcome message.
*Looking for a book on C#?
amzn.to/2IgVThZ
ObservableObject.cs - pastebin.com/AjS3UdVK
BackgroundViewModel.cs - pastebin.com/45gJniJ8
PersonViewModel.cs - pastebin.com/ALgRmGfJ
MainViewModel.cs - pastebin.com/nHd1Esk1
MainWindow.xaml - pastebin.com/EyMM60Eq
MainWindow.xaml.cs - pastebin.com/zZMLSRdh
How did you feel about this tutorial?
www.opinionstage.com/tosker/in...
*Support me on Patreon
/ toskerscorner
** Tip Jar **
Paypal: toskerscorner@outlook.com
BTC: 3DkvwvcQ8Vt2U84jdtEhNBx2Ueai3Xttmu
ETH: 0xFf79A145e1ED6F538607Ec374968e605cbea758b
BCH: qqpgrnzuhc5hav4e79m8nfhmugs46jtl0ujxcu0tj5
LTC: MHXQmJA3hkwTPeArB9QPNkcLiHQx723yMb

Пікірлер: 85
@ToskersCorner
@ToskersCorner 6 жыл бұрын
After re-watching this video to accurately remake the application for those of you who would like the code source, I felt the need to make a comment. Keep in mind when watching this video I wanted to stay more focused on things relevant to the INotifyPropertyChanged.. so there are many things in this video that I would NOT recommend for the functionality we were creating. I chose to write the application the way I did for the sole purpose of emphasizing the focus of the video, and not to demonstrate anything but that. Hence why I avoided DataContext binding, Commands, etc.
@Sebastian-lh6bn
@Sebastian-lh6bn 6 жыл бұрын
Just discovered your tutorials and really got to admit, they are brilliant.
@bkkrisi5477
@bkkrisi5477 Жыл бұрын
Your Videos are great. The rate is perfect, and your explanations are clearly defined. Thank you :)
@ukaszzelechowski6493
@ukaszzelechowski6493 3 жыл бұрын
Hello, I'm from Poland. Very good job. 30 minutes of your courses learned me more than paid courses in my language. Finally i understand what it is binding. :D
@ranavitaln
@ranavitaln Жыл бұрын
Your tutorials are amazing! Simple, structured and not too overwhelming!!!
@jameslombard5698
@jameslombard5698 5 жыл бұрын
Sound is very soft, but nice clear and simple example. Easy to follow and understand. Thanks.
@80amnesia
@80amnesia 5 жыл бұрын
basic (very important), clean, clear and simple examples. Thanks
@mohamdalmhde1322
@mohamdalmhde1322 4 жыл бұрын
Amazing Tutorial!! Thank you so much!
@AZOHA
@AZOHA Ай бұрын
Really nice and smooth tutorial
@native-nature-video
@native-nature-video Жыл бұрын
Thank you for such a clear example!
@Fannypacksnkittens
@Fannypacksnkittens 3 жыл бұрын
Thank you so much!! These videos have been so helpful for me!
@MisterThug187
@MisterThug187 5 жыл бұрын
What a genius , great Job Joe
@noclicheplease
@noclicheplease 7 жыл бұрын
Thumbs up for the pleasant sound of that mechanical keyboard. And thanks for this useful tutorial.
@ToskersCorner
@ToskersCorner 7 жыл бұрын
Preetham S Gowda Thanks for the comment. Glad you like the mechanical keyboard sound as well. I myself find the noise very therapeutic. Would never own anyother type of keyboard!
@InvitusCode
@InvitusCode 7 жыл бұрын
Amazing Tutorial!! Thank you so much! =)
@ToskersCorner
@ToskersCorner 7 жыл бұрын
RapiersRevenge thanks for commenting! I hope my content continues to be of use, and as always, feel free to email or message me on KZbin for any questions or suggestions. Thanks :)
@67Keldar
@67Keldar 3 жыл бұрын
Awesome… loving your videos 👍
@CanadianMang
@CanadianMang 2 жыл бұрын
Thank you so much for these tutorials. You are one of the very few that actually explain WPF and databinding in detail. Most people just rush through tutorial without explaining the details.
@piak15
@piak15 3 жыл бұрын
Your tutorials are beyond amazing! Simple, structured and not too overwhelming. I need this for my studies rn. Thanks alot man!
@fastscript1343
@fastscript1343 2 жыл бұрын
True
@shaikhzoyeb
@shaikhzoyeb 6 жыл бұрын
thanks , i understood very easily
@lionciomorcilla3045
@lionciomorcilla3045 3 жыл бұрын
THANKYOU SOMUCH!
@vvalentinee168
@vvalentinee168 4 жыл бұрын
Thank you!
@studstud5498
@studstud5498 7 жыл бұрын
Oh after few minutes the videos were clear.
@lucasrodrigues3041
@lucasrodrigues3041 3 жыл бұрын
thanks!!
@cyukore1796
@cyukore1796 7 жыл бұрын
very nice tutorial
@BenedictoAlvarez
@BenedictoAlvarez 7 жыл бұрын
Take a break from the tutorial of the vending machine because I did not understand the inotifypropertychanged I hope with this tutorial I became clearer. Thanks for your tutorials
@ronancasey
@ronancasey 7 жыл бұрын
Same with me. How is WPF going for you?
@BenedictoAlvarez
@BenedictoAlvarez 7 жыл бұрын
I need the code because not working for me :(
@ronancasey
@ronancasey 7 жыл бұрын
It's in the link in the description.
@ToskersCorner
@ToskersCorner 7 жыл бұрын
If you guys haven't checked out my notepad wpf tutorial, I think you should. It takes a few steps further than the vending machine tutorial.
@lancechen4001
@lancechen4001 7 жыл бұрын
its helpful
@Whoisemmanuel
@Whoisemmanuel 7 жыл бұрын
You probably already know this but in case not. To create a full property fast you can use propfull and then hit tab twice.Thanks for making these videos I recently got a job that uses WPF and your notepad videos helped to fill in some gaps of knowledge.
@bouncebounce5549
@bouncebounce5549 7 жыл бұрын
Hi Tosker. Instead of typing the method name in the events property dialog, you can simply double-click the empty field and it will appropriately name and create the method in code behind. You mentioned looking for a better way in the video, so I hope that helps.
@ToskersCorner
@ToskersCorner 7 жыл бұрын
Thank you very much for the response. I do know that it will automatically name it. I was unsure if there was a way to create multiple events at once without the need to go back and forth from the event window to the code-behind. Thank you though, because I should have mentioned what you just said in the video for beginners to know of this feature. Hopefully they'll see this comment.
@eitzhadas
@eitzhadas 7 жыл бұрын
Typing "Click=" in the XAML and hitting Tab twice should automatically create the event in the code behind and its name automatically populated in the XAML. F12 then brings you direct to the code behind.
@ToskersCorner
@ToskersCorner 7 жыл бұрын
Thyanks!
@JymMorton
@JymMorton 7 жыл бұрын
Double click the button in the Designer (VS 2017) adds the xaml and code behind for the click event.
@JymMorton
@JymMorton 7 жыл бұрын
I see in xaml there is a DataContext="". Is it not possible to set the colour with that instead of using the Click Event? (I guess I could actually type out your project and see for myself but I thought I'd ask first)
@supercreate4
@supercreate4 4 жыл бұрын
If i include second property in PersonViewModel class then how can i initialized them
@jackdaniels2160
@jackdaniels2160 3 жыл бұрын
Your Tutorial is great. Very helpfull. But your voice is very silent. So... a bit louder would be helpfull. ;)
@juanclopgar97
@juanclopgar97 3 жыл бұрын
how you know when you have to use Window Datacontext or "local" datacontext, (in the control itself)
@brandonhacksandtrainers9246
@brandonhacksandtrainers9246 7 жыл бұрын
Hey ToskersCorner thank you for your awesome tutorials ... I didn't understand what is "mode = " can you please explain it to me.. .thanks again ;)
@martinlym7511
@martinlym7511 2 жыл бұрын
Hope someone can answer this, why does the text change in response to the button being pressed when the button isnt even connected to that textbox? I've also noticed it changed when you press the background buttons. Thanks ToskersCorner, awesome tutorial!!!!
@Cortexikid
@Cortexikid 4 жыл бұрын
Hi! First of thanks for these videos I learn a lot from them! I have a question now, at 8:45, in PersonViewModel, OnPropertyChanged is called and a string was passed as the parameter but I don't get why it is "Name" (with quotation marks) and not just simply Name?
@petarjukic6841
@petarjukic6841 3 жыл бұрын
I believe the name of the changed parameter is meant to be passed, thats why it's "color" and "name"
@edouardpoitras810
@edouardpoitras810 Жыл бұрын
If you look at INotifyPropertyChanged, you will see that NotifyPropertyChanged takes a string as its argument.
@randypenajimenez3893
@randypenajimenez3893 2 жыл бұрын
Is it recommended that model implements INotifyPropertyChanged?
@jackkra5847
@jackkra5847 7 жыл бұрын
good tutorial, but i have a question, how much or what code can you have in view.cs, that you still don't break mvvm pattern. can i use set datasource and simple events in view.cs file without breaking mvvm?
@ThisCanNotBTheFuture
@ThisCanNotBTheFuture 6 жыл бұрын
I'm not sure there is a definitive rule. In my experience the view.cs file can (and probably should) contain things like objects that are inherited from a parent view which instantiates the current view, which (if necessary) can then be used to initialize properties in the corresponding view model. Simple events like tooltips could also go in the code behind. And yes, you set the data context (to the corresponding view model) either in the view.cs or in the xaml. Above all else, you want to avoid service calls in the view.cs.
@nero1988
@nero1988 2 жыл бұрын
What is the point of having viewmodel MVVM when you dont use commands but code behind window
@juanclopgar97
@juanclopgar97 3 жыл бұрын
I love your content and I learned so much but, can you upload new content talking about the Model part?, that would be helpfull
@portaautomat8558
@portaautomat8558 7 жыл бұрын
hi. Thanks for video! very nice. I have one question. Why did you create PersonViewModel and BackgrounViewModel classes ? In normaln MVVM way it shouldn`t be PersonModel and BackgroundModel instances in MainWindowViewModel ? By making more viewmodels classes (like thouse two) it is more complicated. Could you be so nice and explain it to me ? thanks
@ToskersCorner
@ToskersCorner 7 жыл бұрын
MVVM is just a pattern, and you can make your own judgment calls for the most part. In this video, I just wanted to demonstrate using the INotifyPropertyChanged. This would also depend on how you are building the application. Some people prefer notifying property changes in the view model, and some prefer it in the model. I usually prefer the view model, which is why in the case I used view models.
@ToskersCorner
@ToskersCorner 7 жыл бұрын
This depends on the binding. If you notify the property change of the model object only, then when you change it's 'Name' or 'Age' values, the view wont be notified of this. In this case the view would only be notified if the actual 'PersonModel' has been directly set, rather than its inner properties. I hope I am communicating this clearly.
@mr.es1857
@mr.es1857 6 жыл бұрын
Tosker i need you help!!!, it happens to that when i run my program works well i think because it binds to the text block the contenent of the get method of the textbox correctly but when i change the content of the textbox the text block information is the same as it was. get { //if (string.IsNullOrEmpty(_userBox)) /// return "my Friend"; return _userBox; } set// set method - what is wierd to me that the "my Friend " text is binded correctly!
@ToskersCorner
@ToskersCorner 6 жыл бұрын
DOSESTACIONES2E have you tried changing the binding mode of the textbox xaml to "TwoWay"?
@mr.es1857
@mr.es1857 6 жыл бұрын
well think i was in OneWayToSource i don´t remember know and i solved the problem making a property of the label and notifing but i dind´t like it because i don´t think that that little thing that could take the source from the text box entersmy view model to make it little larger. Soo i was trying to the way you did and i was not using a twoway because if "My friend" was the default text on the label making it twoway, the textbox showed "my friend" too instead of the watermark soo i put a one way to the source. -Somebody told me that you were doing something extra that you dind´t told us to do that because he said that you were never notyfing the label to change at first it was logic for me but i don´t kind of belive that So if could give some advice i would that you a lot tosker!!
@ToskersCorner
@ToskersCorner 6 жыл бұрын
It's sort of difficult for me to understand your problem because of a language barrier. It sounds like English is not your primary language? Also there may be some mistakes or unexplained things in my videos, so don't always assume you're wrong or I am right because I show something a specific way. Always verify things you learn from multiple sources. There are a lot of opinions and methods out there. This is true not only with my content, but content virtually everywhere. Even MSDN has poor examples and explanations sometimes. If you provide me with code-examples it may make it easier for me to understand what you are asking. You can always use things like hastebin.com for your code and email me your questions (Toskerscorner@outlook.com). It might be easier to help you this way, rather than KZbin comments. Also, in regards to the last question. Make sure you use the UpdateSourceTrigger in the xaml.
@windowsbuilderthegreat3121
@windowsbuilderthegreat3121 3 жыл бұрын
You should do a video on user control inheritance
@jayjayjaythebrand
@jayjayjaythebrand 6 жыл бұрын
Was your site hacked? Looks like the source code isnt available for download anymore.
@studstud5498
@studstud5498 7 жыл бұрын
yes please. this video has no sound and also I cannot see the code when you explain.
@pokerspins
@pokerspins 5 жыл бұрын
In MainWindow.xaml, replace both Person.Name by just Name. Only works this way for me.
@nicklaspersson4687
@nicklaspersson4687 6 жыл бұрын
Good tut. But hard to see with this tiny font size with grey on black. Sunshine in window and somewhat aged eyes.
@ToskersCorner
@ToskersCorner 6 жыл бұрын
Nicklas Persson Thank you for the comment. Unfortunately video editing is just a particular weakness of mine. I do try to improve on it in my future vids, just doing trial and erroratthis point.
@BenedictoAlvarez
@BenedictoAlvarez 7 жыл бұрын
Toskers can you put the code for this example in github
@BenedictoAlvarez
@BenedictoAlvarez 7 жыл бұрын
please put the code in github
@zubair007khan
@zubair007khan 4 жыл бұрын
Crud App please
@BenedictoAlvarez
@BenedictoAlvarez 7 жыл бұрын
excuse ToskersCorner in this line
@ToskersCorner
@ToskersCorner 7 жыл бұрын
Ah, yes you are correct. You caught a syntactical error, good catch! I'll update that soon, thanks for letting me know!
@redslayer973
@redslayer973 6 жыл бұрын
thanks for this useful tutorial and please fix the link
@ToskersCorner
@ToskersCorner 6 жыл бұрын
Added links in the description for paste files for the code used!
@risncch6595
@risncch6595 4 жыл бұрын
where is the sound?
@studstud5498
@studstud5498 7 жыл бұрын
could you show me with database
@ToskersCorner
@ToskersCorner 7 жыл бұрын
Do you mean property notifications when a database has changed? If you are thinking of something like "push notifications" I haven't myself looked into that with WPF. Otherwise I would think of something like a service layer that just checks the DB at a certain interval.
@karamazov9610
@karamazov9610 3 жыл бұрын
Nice, but the volume is really low.
@DJnoratos
@DJnoratos Жыл бұрын
Why every time I watch some programming related tutorial, the creator is not capable of making reasonable video volume ???
@leniedor733
@leniedor733 3 жыл бұрын
LOL at 16:00 start using a non-MVVM pattern? Dont know why you talk about "consistent" when writing code on View part is a non-good practice at all... I really hate using code on view (programmatically) because when you're using a pattern to code just stick to it plz. Now ppl could just copy your example and using at work and that's nasty when others see it. And the other parts are awesome, keep going bro! i just gonna sub for the other
@STUPIDYOUTUBE_HIDINGMSGS
@STUPIDYOUTUBE_HIDINGMSGS Жыл бұрын
Audio is too low...
@EnotoButerbrodo
@EnotoButerbrodo 3 жыл бұрын
Не работает
@quantomic1106
@quantomic1106 4 жыл бұрын
Please don't hide your mic under the keyboard.
@MikePL101
@MikePL101 3 ай бұрын
The audio is terrible on this video, can't hear what you're saying unfortunately.
@DJnoratos
@DJnoratos Жыл бұрын
⚠ Do not follow this tutorial code-style. Newer make duplicit variables like he does with the _color, etc. as it is completely redundant... 🙄
C# WPF - Commands [Part 1 Intro + Singular Commands]
17:32
ToskersCorner
Рет қаралды 50 М.
WPF INotifyPropertyChanged and Databinding
14:30
Payload
Рет қаралды 39 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 22 МЛН
Heartwarming Unity at School Event #shorts
00:19
Fabiosa Stories
Рет қаралды 25 МЛН
Inside Out Babies (Inside Out Animation)
00:21
FASH
Рет қаралды 23 МЛН
C# WPF - Relay/Delegate Commands [Part 2]
21:25
ToskersCorner
Рет қаралды 33 М.
Switching Between Multiple Views in MVVM - EASY WPF (.NET CORE)
12:29
The Basics of Data Binding in WPF
20:53
Tactic Devs
Рет қаралды 38 М.
C# WPF Tutorial - ComboBox, ItemSource and Templates
15:37
ToskersCorner
Рет қаралды 51 М.
WPF C# Professional Modern Flat UI Tutorial
36:44
Payload
Рет қаралды 804 М.
Understand your C# queries! IEnumerable & IQueryable in explained
11:28
tutorialsEU - C#
Рет қаралды 33 М.
What is the use of INotifyPropertyChanged Interface
6:46
High-Tech
Рет қаралды 10 М.
Best Toilet Gadgets and #Hacks you must try!!💩💩
00:49
Poly Holy Yow
Рет қаралды 22 МЛН