DataTemplates in WPF

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi guys now in this lecture I will make you understand what our data templates in WPF so like control templates data templates - are responsible for visualization but in this case for visualization of data now since you have learnt about control templates what contained control templates enable us to do they help us to change the look and feel of a control that is we can visualize our control with the help of control templates same concept goes with data templates we can visualize our data with the help of data templates let us try to understand what I mean by this through an example so here you know in my example I have a simple label control which displays I am a simple label displaying string text and let me run this application you can see I am a simple label displaying string text now since this content here is just a simple string so WPF engine understands this string text that is why this string text is being able to be displayed without any data template now say suppose I have an object okay now object is a complex type and say suppose in this content property I need to bind a property of an object so in that case WPF engine would not understand the representation of that object that is it will not able to display that object onto the screen let us try to understand what I mean by this through an example so let me make a simple class so that I can have a complex object and let me call this class at as person class and let me make it a public class okay and let us have a property of type string that gives me the name of this person and let us make an object of this class of type person okay and on the constructor I will initialize this object so VJ equals new person now I will set this prop name property of this object as say with art Kohli right and let me set that data context of this window this not to context because this so I am setting the data context of my main window zml to this code-behind of this window if you are new to data context class you can watch my video on data binding right so I have a very simple class that has a name property which displays the name of this person and what is the name of this person the name of this person is we are calling right now let us get back to our main window and instead of this content displaying a string content let me write this binding marker then bind it to my obj great so what I am doing I am binding this object that I have created Hin here was neon whose name is Vera Kohli to the content of this label now let us try to run this you can see what is the result data template dot demo dot person that means it is giving me the fully qualified name of this object that is the namespace of our application is data template demo and the name of the object is person so it gives me data template demo dot person now why am getting this error is because WPF does do not understand how to represent the object onto the screen it just understands how to display string content onto the screen so in our previous scenario that string content was displayed on to the screen but if we are if we are displaying an object WPF does not understand how to display that object so smoke back to slides so if we want to represent an object into WPF saml window we should use data templates now data templates are very common whenever we work with data bindings of object and that's what we are doing in our example we are trying to bind the object to our screen and we are not getting desired output so how do we remove that problem there are actually two ways to do remove that problem let me show you the first so let me stop running of this application now what this WPF engine is doing since it does not know how to display this person object to the window it's by default calling the tostring method of this person class so we can do a override to this two string method and we can always return a string that says me wait now let's try to run this you can see we get miracoli in here so our object is displayed correctly so what has happened since WPF does not understand the object to be displayed onto the screen it has called the all object dot to string method to my object and what we are doing this as a solution we are overriding this that to string method to return the correct formatted string so let us move back to our slides this what mentioned this is what is mentioned in the fourth point if we do not define a data template WPF does not understand how to represent the object onto the screen and therefore it calls dot to string method on that object now we can display this object by overriding the to string method but this becomes tough if we are working with many objects say suppose I have 50 objects in my application I need to call I need to override that to string methods 50 times so the solution of this problem is using data templates so let us try to use a data template and see how can we bring virat kohli on to the screen so let us move back and let me remove okay let me comment this out that you can and let us try to run again and see we get the fully qualified name so let us get back and let me use the data template to correctly display this so let me make label dot ten template and this is the mark-up for using data templates data template going to always be defined inside a content template markup okay let me data template now this data template should also contain a label to hold my property so label the content should be bind the name sorry and let me close this label and let me close this label the upper level let me scroll this down so that you can see and now let me try to run this and it runs as Bharat Kohli so it gives me recorded to the screen now you can see I have foreground red and for my dashboard define in here to this label but what is what it is actually displaying it is displaying the data templates label that is this label so let me cut this and paste it in here and now run this and you can see where our Kohli is displayed so my object is being displayed correctly to my screen with the help of data template and actually this label is what being displayed now inside a data template I am just binding this obj object to the outer label but I am binding the property to the inner label now this really does not make sense because Duke phi2 use a label and then we use a label inside that label right so that is the reason why we use content presenters now I could have started this tutorial right with the help of content presenter but that would make made the bigness confused that why we are using content presenters instead of it label or a text block so here we go I have changed my label to content presenter so this content presenter is holding this label inside it inside a data template so let us try to run and you can see where our coal is been displayed correctly this label is actually playing the object inside our data template and the outer holding job is done with the help of this content presenter we could also use a text block in instead of a label but other reason why I use the label is because label has a Content property and text block does not have a Content property it has a text property so if I would have started this example from a text block I needed to explain that why I am not using a text block because it does not have a Content property so that would have made this video so complex for you again let's try to run this and see if it works yes it works expectedly Soviet fully is being displayed on a label on decks block on each and everything now we can also generalize this thing with the help of a window resource I can declare my data template inside a vendor window this was tagged so that I can use it again in again let me cut this data template from here to here and let's give this a key just like I am I have given all the keys to my Styles so let me name it my template and let me remove this content template tag from here and set its property here contain template and let me bind it through a static resource markup extension okay so it's the same concept what we use the concepting styles and let's try to run this see it runs as expected so I can use this data template again and again because this is being declared here on to the window resources section so this is all about data templates data templates helps us to visualize the data on to the correct form its move back to our slides so let's look at the last points database could be declared as a resource and this we have seen so water data templates data templates helps us to visualize the data those are of complex types now data templates are widely used in data binding concepts and whenever you work with real time projects you will use more and more data templates with your application to correctly display the data so thank you guys for listening I hope this video has helped you now please do subscribe to my channel and keep learning keep Rocking if you have any doubt please leave a comment thank you so very much
Info
Channel: DotNetSkoool
Views: 45,386
Rating: undefined out of 5
Keywords: C# Training, DotNetSkoool, WPF Tutorial, WPF Training, Templates in WPF, Data Templates in WPF
Id: s4ABnjHMyg0
Channel Id: undefined
Length: 12min 58sec (778 seconds)
Published: Mon Aug 01 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.