‫Welcome back.
‫In this video we are going to talk about I notify property changed so about this interface which allows
‫us to trigger something if a property was changed and we have seen property changed in the last video
‫when we were using binding and we're going to do it slightly differently this time and you will see
‫how to use this interface yourself.
‫So instead of a grid, I want to use a stack panel because I want to stack things up and I'm going to
‫stack up, first of all, a label which I'm going to set the contents to, number one, and I'm going
‫to close this label like that.
‫And below that I want to have a text box where we can enter a value.
‫So I'm not going to use a text block a text box here.
‫I'm going to close it right away like that.
‫And in the text box, I want to have the width of 100 pixels, a height of let's say 30 pixels.
‫And then I want to set the text.
‫Now the text should be bound, so I'm going to use data binding here.
‫And the path that I want to use is going to be number one and the mode is going to be two way.
‫So we've seen what this mode means and what this path means.
‫So now let's copy this code and duplicate it here.
‫So I'm going to copy and paste it twice.
‫So this is going to be none too same here and this is going to be a result.
‫So this is going to be the result and this is going to be number three.
‫So now you might wonder, what is this number one?
‫Number two and result, because, well, we have number one and number two and so forth.
‫But these are just contents.
‫They are not names.
‫So in order to connect to them or use them, we need to go into our code behind because I want to create
‫those in here.
‫So I'm going to create a new class actually, because we're going to use the main window as our program
‫so similarly to our program.
‫But I want to use I notify changed and in order to use this interface, I need to create a new class
‫that can inherit from it.
‫So I'm going to use a new class here and I'm going to call it some because what I want to do is if we
‫go back to our example file, we have this num one, num two and number three.
‫And what I want is if I add a value here and a value there, then the value here at the bottom should
‫automatically be the sum of the two before.
‫All right.
‫So I'm going to use a new class for that.
‫And this class should inherit from I notify property changed.
‫And in order to do what?
‫To use this I notify property change.
‫We need to use the system component model.
‫So I'm going to use using system component model.
‫So now you can see our interface is green which is the well generally this light green which is the
‫interface color in Visual Studio and now I can fix those potential errors.
‫So you see I need to implement the interface.
‫So I'm going to do that.
‫And as you see, we have this public event, property changed, event handler property changed.
‫And now in order to use that, we will need to create a private method called void on property changed,
‫which is going to take in a property.
‫And in my case, this property is going to be a string.
‫And I want to check first if property changed, property changed is not empty, so not equal null.
‫Because if that's the case, then I want to go ahead and call the property.
‫Property changed.
‫This dot new property changed event arcs, which I'm going to hand over to properties.
‫So the property changed event arcs, as you can see.
‫Needs a string property name.
‫And the property name that I want to hand over is the property itself.
‫So on property change is going to call this property and I'm going to enter a property in here.
‫All right.
‫So we will simply call this property change event.
‫And now, in order to use all of those, we need to have several properties.
‫And I'm going to start off by creating private fields first.
‫So a private string num one, then a private string num two, and finally a private string called result.
‫And now I create properties for all of those.
‫So I'm going to create a property called NUM one.
‫And this number one property should be slightly different than the standard one.
‫So I'm not going to use this basic get and set, I'm going to use a self defined, so I'm going to define
‫one myself and it should not return num one, but it should return well, not the one with the capital,
‫not the property, but actually this field here.
‫So it's going to be renamed returning num one.
‫So return num one.
‫And if I set something then I want to set the value.
‫By the way, I use string here at the top, so my property should also be strings.
‫So I'm going to return num one.
‫And now if somebody tries to set num one, I want to try to parse that because what if it's not a number
‫that the user has entered?
‫What if he entered, I don't know, letters or something like that?
‫Then I want to catch that and change it.
‫So I'm going to use INT number and I'm going to create a new boo here which I'm going to go result is
‫going to be int, try parse and I'm going to try to parse the value and I'm going to get get out the
‫number.
‫So we have seen try parse before.
‫What it simply does is it tries to parse the value that is given into an integer and if it worked then
‫it's going to be true.
‫And if it didn't work, then it's going to be false.
‫And by the way, if it works, then we'll get the number as out as well.
‫So if I can or if I get a positive value now I'm just going to say num one is going to be the value.
‫And also what I want to change or the trigger is the property changed and I'm going to trigger the NUM
‫one.
‫All right.
‫So I'm going to change this num one property if the value is changed.
‫Now what we also can do is just copy that for our second number.
‫So num two and I'm going to change that to num two.
‫It's also going to be a number and here it's going to be num two and there as well.
‫And now finally we need a property for our result.
‫So I'm going to create that public string result and this one is also going to have a get and a set.
‫So here a get is going to have an result is going to be intact pass number one.
‫Plus ain't.
‫The Parrs mum two.
‫And why can I do that now?
‫Well, because I'm returning them to and whenever I'm setting them one and I'm two.
‫So the ones here, they are integers already because they are only set, they only have a value if they
‫are a number.
‫So otherwise they will never be set.
‫They will be empty.
‫And then what we need to go ahead and do is to return something in here.
‫So I'm going to return rest to string.
‫So the result and now if somebody tries to set the result, I'm simply going to do the following.
‫So set int result is going to be int pars.
‫Actually the same thing that we did here, only that we don't return it, but we set it.
‫So result is going to be rest to string.
‫And now in here we call the UN property changed for our result and the same thing goes for our num two
‫and num one.
‫So what that does.
‫So this UN property changed.
‫As you can recall, it changes the value of the property.
‫So property changed event arcs and it's going to take in the property and it's going to call this property
‫changed event and then all of those properties.
‫So the number one property, the number two property and the result one will be notified and our result
‫should be changed.
‫So what I want to have is every time somebody enters a number that this number three is going to be
‫changed as well.
‫And before we can check that or test that, we need to go into our .0.6 file and actually create an
‫object of our some class.
‫So we have created some class and I'm going to go ahead and create a new object.
‫I'm going to create it up here, which is not just going to be an object, it's also going to be a property.
‫So some object is going to get and set.
‫And now in order to access that, we need to make our some public as well.
‫So let's make this class public and now we can go ahead and use this some object.
‫Now I can set this some object, some object here at the start.
‫And I'm going to say it's a new sum with the value of not one being one, and number two, just having
‫a value of, let's say three.
‫And as you see, we have to use strings here because otherwise it won't work because those properties,
‫they are strings, right?
‫So now what I need to call is or what I need to set is set the data context to our sum object because
‫otherwise nothing will happen.
‫So now let's run this.
‫And then you see, we never set number three, which is the result, and maybe it's even better if we
‫actually call it result.
‫So let's go here and change the label to result or maybe even some.
‫So this is going to be the sum.
‫Maybe it's even something like that of non one and non two.
‫So now let's run it again.
‫And there you are.
‫One plus three is four.
‫So now let's change it to, let's say, 25.
‫And as you see, it changed.
‫So now let's change the value of 3 to 31.
‫You see 56.
‫Now, if I change that value here.
‫You see, it's going back to its normal value because what we did here in The Setter.
‫If someone tries to set the result, we simply set the result to, again, the value of end parties,
‫number one, and number two.
‫So it's always going to go back to the actual result if we ever try to set the result of our string
‫result.
‫So now you can play around with this and you can calculate whatever you want and you will have real
‫time answers or real time well sums of your values.
‫You could go ahead and add another one, or you could now go ahead and change that to be a multiplier.
‫But now I know that was quite some setup that we had to do.
‫And so I want to go through it once again real quick with you.
‫So in the XHTML file, we had those three labels and those three text boxes, which are just the UI
‫elements, right?
‫And the text of each of those is connected.
‫So this one has the path to num one, this one has the path to them two, and this one has the path
‫to result.
‫And those are those ones here.
‫So the num one.
‫The num two and the num three properties.
‫We created an extra class which is inheriting from I notify property change, which is an interface
‫which has this property changed event handler which is an event and we'll just call property change.
‫Now we created a method called on property change, which is called each time that one of our properties
‫is changed, and that is done once a property is set.
‫So we have this property num one and if somebody is trying to set that value of NUM one, we are simply
‫going to call this on property changed.
‫And in here we set the property so we say, okay, give me a property and we change that property to
‫the value that was given.
‫So it's pretty much we set it, but at the same time, we don't only set the one that we are currently
‫setting in terms of in the setter.
‫So we don't only set num one here, but we also set the result and we don't set the and here you see
‫this line here it sets this field so it changes this private string field and this on property changed
‫actually changes the property itself.
‫So it's going to change the value of the property itself, but at the same time it's going to change
‫the value of the property result.
‫So it's going to update our result each time that we change the value of num one and the same goes for
‫num two and the same goes for our result where I said each time that the result is changed, we want
‫to go ahead and call this on property change method as well.
‫Right.
‫So now you see how you can use AI, notify property changed how you can use this interface and you can
‫use it in multiple different ways.
‫And it's really interesting to use it whenever any property is changed.
‫So let's say the colour is changed and you want to trigger an event that's going to do some fancy animations
‫or somebody presses a button which will change and a property somewhere in your program and you want
‫something great to happen afterwards.
‫You can set that all up with this.
‫I notified property change interface.
‫What you, of course need to do is to create a inheritance correlation here between the class that you're
‫using and notify property changed.