 Welcome to the network engineering video blog. I am your host Michael Crane. So in this video we're gonna look at changing our data source for this router's name. We're actually any of our class node names. And before we get started I was using the documents. I started with this binding source overview because what we want is we want to bind to a class, right? And so I kind of looked at this binding sources overview and I kind of messed around for a little bit. So if you read implementing a class for binding source it kind of it kind of takes you down this weird recommended mechanism is to implement this I notify property change interface and all this other stuff and and I started going down that rabbit hole and I'll show it to you. But yeah that I don't that wasn't actually necessary. So so then I also was looking at this document here and this is part of the rabbit hole I was going down is how to specify the binding source which gives you this okay this custom class and they're implementing all these property change events and they're manually they're manually coding the get and set for the properties. And but this note right here actually is what saved me. And I guess we can look at it real quick it says above example standshates the object and markup and use it as a resource if you want to bind to an object that already has been instantiated in code you need to set the data context property program programmatically and then it says for example see this make data available and I believe that's this tab right here and and of course you know it's how to make data available for binding an XML but you have to kind of read all the way down till you get to oops if you define now here we go right here so it's however if you are binding to an object that has already been created you need to set the data context and code and that's pretty much it okay okay so why am I doing this well the the title as you probably know for this video is binding to a class object so and I and I know what an object is a instantiated class so it's I don't know is that redundant I I don't know anyway maybe I should just show you okay so we have our our class node properties and if you've been following along in the videos you're familiar with this it just had this in there right it had actually these weren't in there it had index category make model left and top all right and that was pulled in by our class node which is this guy right here and so these were the properties for our class node which is our which is our parent class for our router nodes and switch nodes and that sort of thing right so but the the name that we're using I mean find the GUI here so the name we're using right here this router underscore zero was actually a name property in let me find it in this framework element class well let me here's class node I'm sorry in this user control right which is which I believe is a framework element property but anyway let's let's so it's it's pulling it in from user control and our main window is using this as as a key for our collection of nodes right so I was putting the router node object and using that the name as a key okay and but I wanted to be able to have the user when we go to edit this I wanted the user to be able to change the name so I didn't want the where's name in here make model well it's not even listed in here am I missing it anyway I guess I'm missing it but yeah or maybe it was now here it is up here I'm sorry so it's right here so here's the name of it but I wanted the user to be able to come in here and change the host name and in the name that's displayed on the router you know okay and so I just called the field nickname I started to call it label but labels a keyword and in visual basic and I I didn't want to have any you know problems with that so I just called it nickname so now what we've got is is if we look in here we've got our nickname and I added hostname I didn't really do anything with hostname now so we can just ignore that okay so let me do a couple diffs and we'll we'll see what I changed all right okay stop this all right I guess we'll start with the main window seems like the best place to start and and you can see right here so when we when we do this add router now here's the name property that we're taught that we use as a key right and and then I just I just I default the nickname and the host name to this name property you know for why not right okay and then I just did some debug stuff and I just did this I just duplicated this for the switches and the test sets all right so that's all the changes that are in the main window all right and that's just populating this class node properties right or it's initializing it I should say okay so then if we go to and we're just gonna look at the router node so we'll look at the router node XAML and if we come down here I did a couple things one so on our text block now originally it was just it's just a text block with a margin foreground and it was binding to an element name called XAML name that was defined up here and we were changing it programmatically at runtime right and that probably wouldn't the greatest thing to do but it worked right so and that's okay you know this worked and it was okay but like I said I don't want to keep changing this right here because we use it as a key to do searches on but I want like I said I want the user to be able to change it so I gave the text block a name so we could access it in the code behind and then I changed the binding from this element name and our control name to this nickname right here all right and that's I think that's it yep so if we go into our code behind what did I add here now this has got a bunch of comments in it so you can just ignore it but the only thing I had to add is this so here's the name that I added in the XAML right for that that text block and then I just set as data context to our properties object and the properties object as is instantiated in the in the class node where is it class node so here so here's where it's instantiated so we we got a prop a property called property prop and it's a class node properties which is this guy right here and whoops and and that's it and so that's all this where is where are we right here okay in the router node yeah so I just said okay that the the text block label data context is just using this object property right and then in the XAML I can just say bind a nickname okay and that's and that's that's pretty much it but I'll tell you what I was you can and that's why I left the comments in here I started to go down this this rabbit hole of okay you know here we want to you know define all our gets and sets and I forget where the other one was but it was oh yeah I had to make all these events and you know implement this I notify change and I was like on this can't be that hard right and what's funny is we've used this data context before but I it slipped my mind and I didn't even think about it but yeah this and and if you're wondering if it works if it if it's a true data binding it does because it is because in our main window right here here it is so here's where are we Canada's children add router so oh yeah so here's where we're creating a new Cisco router node so the XAML is getting instantiated or initialized I should call it up here right and and if and and we're changing its name right there or changing its nickname anyway okay and so and so we're changing it in this property file and it's getting pushed without any of these events down to our XAML right so and I don't know what the default binding is for this I think our mode I think it's a two-way I don't know I I think if you leave it blank it you know so if you do like mode equals so you've got default which I think is if you leave it blank and then you got one way two way one way to source two way and I wanted one way but I decided just to leave it blank so we didn't have any problems and if it defaults to two way it might we might have to come back and change that later because it just adds overhead you know to our program that we don't really need right so don't forget you can support the network engineering video blog by donation using a credit card and PayPal or by purchasing products at the muxall store details and links are in the description under this video well that's about it for this video if you like the video give it a big thumbs up that helps and hit the subscribe button that really helps if you have any questions or comments post them in the comments under this video thanks for watching and I'll see you next time