Creating a custom Dependency Property in WPF

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so in this lecture we will understand how to make a custom dependency property in WPF now I have explained dependency properties in detail in my previous lecture so if you are you have not watched that lecture please go and watch that lecture and then you can continue with this lecture the concept of dependency properties is a very important concept and WPF uses that concept very widely internally okay but there are certain scenarios where you need to create your own custom dependency property in WPF but if you are intermediate developer or say starter in WPF there might be very less possibilities that you need to create a custom dependency property in WPF so this is only intended to or if this is purely based on the requirement of your project okay also there are certain complexities involved if you are designing your own custom dependency property in WPF so you need to implement certain functionality that is out of the box so you need to have your own custom dependency property so what I will do in this lecture I will try to create a custom very simple custom dependency property in order to just youtube under make you understand the concept of how to make your custom dependency properties and if you want to know the concept of water dependency properties then you will need to watch my previous lecture in dependency properties so first let me show you what I am trying to achieve in this example and then I will build that example from scratch so I have a application running in vine system so let us move to that application and you can see this is a counter which is upgrading itself right we don't have any button defined in it to do so so what what this what this counter is doing its pointing change notification to itself and it's updating itself okay and this could be achieved with the help of dependency properties now there are many many different types of ways to do this in various other technologies too but in WPF we can achieve this with the help of dependency properties also so I thought that I might use this example just because it has a very little amount of code behind inside behind it so this will not make matters complex to you so let me close this application and let's try to build this application from scratch using a dependency property so let's move to visual studio and you can see I have a window which has a grid and then that has a view box inside that grid so if you box is a control just like list box just to just for making you to view the control okay and then I have a text block inside that view box so this is the text block and all this text box and text block says that I am a text block so this text block will hold the value of my counter let's move to the code VN of this window and so if you need to create a counter you need a property to hold that value right so in normal scenarios you might do like this you can have a property which is called as counter right and then you can increase that counter one by one in each second using a some timer class or something but we need to do this scenario we need to achieve the scenario with help of dependency properties right so let's make a dependency property for it so you need to go to the prop DP part and then you need to press tab toys you and your property is created here okay so the return type of the property should be integer the name of the property will be counter okay and the name of your property will be the counter property so I have explained you this syntax in my previous lecture okay so all you need is is to have a type of that is the irritant type of this property and then you need to define the owner class and the owner class is the main window class of my dependency property so you can see this is the main window class and our dependency property is defined inside this main window class okay so here is the dependency property so I will increment this normal property with the help of this dependency property okay so this normal property wraps this dependency property inside it okay so this is the wrapper and this dependency property is the variable okay now all I want to do is to increment this counter property periodically okay so what I will do inside the constructor of this main window I will have dispatcher class so if you need to define the dispatcher inside wp8 I'm on inside WPF so you need to use dispatcher class so dispatcher class is a very different topic and it's a very vast topic to explain you because it takes one it takes the threading I expect of WPF so I will cover that dispatcher - I will make a video on dispatcher right so now you need to just to understand that if you need to use the timer class in WPF easier to use through dispatcher so let's use the dispatcher time class and let me mean the instance of this dispatcher time at last okay and then we will use the the constructor which takes four overloads of this now dispatcher timer class okay so this is the fourth overload which takes a timespan so it will define that how we can increment our counter then we will we need to have the dispatcher priority in it because it was since dispatcher takes part in threading aspect so we need to decide the priority of that thread and then we need to define a delegate which will act as a callback or you can say it will provide the chain notification to my dependency property right so if my dependency property changes so this callback will notify the timer or this class okay and then we need to pass the dispatcher itself okay so once we do this this will be clear so let me first define the time span so time span dot from second so we will need to take this value in each one second okay and then we need to have the dispatcher priority so let me have the dispatcher dot normal so I am just keeping the normal credit why it not getting the intelligence okay so I missed this so I need to use this dispatcher priority class and then I would need to have this normal priority okay and then I need to define a delegate let me define that delegate in a separate line so that you can understand so I will have a delegate okay and this delegate will be the anonymous delegate so inside this delegate I will define a value so that this value will be incremented okay so I will inside this new value to zero then I will then say if counter so counter is the property which I have defined in here so if counter equals in dot max value so if this is equal to max value I am just checking it this new value will be initialized to zero okay and then I will have else part so this will increment the counter so if this is not max value so it will have in and will increment the counter okay and then this delegate will take a set value so this set values the same set value which is been used in here so these are the special methods which are which are used to retrieve the value of dependency properties okay so this will take a set value and you can see what does the set value expects its expects with dependency for property and value so I will pass my counter property dependency property in here so this dependency property is this counter property okay yes and what's the sec it says it wants a value okay so what is the value of my property it's the new value so I am setting my values in the new value variable so I will pass new value and then I will terminate this and then I will have the fourth parameter so this was the third parameter of this dispatcher timer class this delegate event handler callback so I defined it as a delegate and then I will need to pass the dispatcher itself so let me pass the dispatcher dispatcher and let me close this okay so I have defined my dispatcher timer class and all I am doing is just defining a time span from which it will start or for which it will increase the counter I am defining the dispatcher propriety as normal okay and then I am define a delegate which will set the value of this counter okay to counter plus one that's it I am nothing and I am doing nothing complex and then this delegate takes the set value which accepts the dependency property and the value which I have set in here okay and then it takes a fourth parameter which is the dispatcher itself which takes take it takes care of the threading part of this thing okay so my dependency property is ready now let's move to my main window and you can see this is hard-coded in here the value of this text block so let me change this and bind some value or bind my dependency property into it so let me have the binding markup an extension okay so path equals my counter property so I am binding this counter property which I have defined in here so this counter is the normal CLR property which encapsulate say dependency property inside it okay so I'm binding this counter property fine now can you guess what will happen if I run this application I if I will run this application my application won't show an error but it will display nothing that's because I have not sent centered data context of this window to this grid okay that's that's the reason why it will definitely nothing so I will just try to run this and you can see it doesn't give me error but it it's displaying me nothing okay that's because I have not sent sender data context of this window okay not data context is a concept which is related to data binding and a half not covered data binding yet so I will be covering data binding and then you need to then you will understand what is what do you mean by data context okay so for now you just need to understand that we need to set the data context in order to for this binding to work okay so let's set it explicitly so with me QA x call done name to this window so it says it will say my window okay and then I will so I have given the name as my window and then I will say that bind to to the data context of this my window so how do we define it we will define my element name and it will you can see the intellisense is my window it okay so now let us try to run this and you can see it's running as expected so my counter property which is bound to this text block is updating itself okay and it's updating per second so let me close this and show you that this is per second so let us try to make it a bit faster let me have opened five passed into it okay let's try to run this and you can see it's updating in in the half of second so it is updating it faster let's play more so let me have a point zero one seconds so it will update it very quickly you can see this is our dependency property working and it's quite fun right and then then also there also is a concept which I told you that I will cover it in the next lecture in this is called property metadata so what is prep property metadata in dependency properties actually this the property metadata is something which defines the internal things of your dependency property so this dependency property metadata this this thing defines the from where this dependency property will start okay so for now it's defined as 0 let me make it as 25 okay let us try to run this or first let me decrease the amount of seconds here so that you can see the counter updating and now let us try to run this you can see our dependency property has started from 25 okay and it has it's updating on per second basis so what does this property metadata do if you hover the mouse it says it takes the default value okay so you need to if you need to pass a default value to your dependency property you need to pass it on property metadata so it depends on your property what type of property you are creating if it's you are creating a string property so it will take the default value of the string class okay and it has four overloads so when your if you need to define certain validations or certain coercion values so you can define it in the property metadata okay so as we will work more and more in dependency properties you can then add validations and some error control event handling something else in the property metadata part of this property okay so I hope you understood the concept of custom dependency property and this is a very simple property what I have made so if you need the code of this dependency property so I will paste that code in the end of this lecture ok so you can copy that code from there from there there so this is all the lecture of custom dependency property in WPF if you have any doubt please leave a comment and please do like share and subscribe thank you so very much guys for listening
Info
Channel: DotNetSkoool
Views: 44,031
Rating: undefined out of 5
Keywords: C# Training, DotNetSkoool, WPF Training, WPF, Dependency Properties, Custom Dependency prop
Id: gtAryodSiME
Channel Id: undefined
Length: 16min 21sec (981 seconds)
Published: Wed Jul 06 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.