.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh

Home >> Articles >> Silverlight >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Implicit DataTemplates in Silverlight 5, With practical case study

Posted By:Manas Patnaik       Posted Date: April 20, 2011    Points: 200    Category: Silverlight    URL: http://www.dotnetspark.com  

As we know Silverlight 5 Beta got released with some exciting features for developers .Based on the the priority and the importance for a LOB application i am trying to cover the features .In my last article we discussed about XAML binding debugging option and in this post we are going to have a look into the new implicit DataTemplete feature .

DataTempletes are visual representation of data .Consider a case you are supposed to show all the Client list in a combo box along with their Phone Number and Email Address , how will you achieve it?? This was practically impossible with previous technology until unless bit extra work around.
Implicit Data Templates
So here do Microsoft offers richness in User experience with DataTemplete concept.I am not going to cover basics of DataTemplete but if you are a newbie to Silverlight and just started on Silverlight then read some basics of DataTemplete and continue with rest of article.

DataTemplete Evolved , Implicit DataTemplete

As we know until now Silverlight Supported applying datatemplete to the controls not Data
but in Silverlight 5 enables the template to attach itself to data object type rather than control only.How ever the same feature is there in WPF since long but Microsoft introduced this concept to web RIA  with silverlight 5 Beta ,naming Implicit DataTemplete.Considering that you have a basic  idea about datatemplete i am going to put forward a practical example with implementation.Let me know if you have any other ideas or suggestions.

Implicit DataTemplete , the Key Points

  • Implicit Datatemplete means template for Data Type not control.
  • No or Zero dependency on Control type
  • Useful for Composite collection , Collection with Different Data Objects

The Case Study

Lets consider a case study for a HR of a company , for whom you are planning for LOB application ,
I am the HR executive of a organisation and my job demands employees information on my finger tip.I have a application that shows up the list of my employee with their respective information.And based on our policy the employees can be of two type
  • Regular Employee
  • Hourly Employee(Contractual)
The Regular Employees have salary option on monthly basis however the contractual employees have remuneration on hourly basis.Also the regular employee are going to have company domain mail ids.
Based on above requirement the class diagram comes up with somehow as bellow

Implicit Data Templates

Now the one of your page requires to show all employees (With in a collection of Employee) with their information .And the problem here with earlier approach is that your DataTemplete for the list box must vary based on the data and also the visual representation for employees must be same irrespective of controls ,either in List or Combo box.Here Implicit DataTemplete comes to our rescue.

Implementing DataTemplete

As mentioned above , i am going to load all employees both Regular and Hourly to the list box .The List box has no idea how the data is going to display.In first step i am going to define a DataTemplete in App.Xaml Resource.As earlier we used to do, the DataTemplete usually carries a key and the key used to be assigned to control .But ..
Here in implicit method instead of Key we are going to add DataType attribute to the datatemplete.

Implicit Data Templates

Make sure that you have imported the namespace of the types you are going to use.Here my emp indicated my namespace for EmployeeClasses.

Implicit Data Templates

So as you can see i have two datatempletes defined for 2 different type such as HourlyEmplyee and RegularEmployee.Each of the template defined in such a way that the employees can be identified easily with in a collection.

A typical piece of DataTemplete Xaml wrapped inside a stackPanel and Grid ,can be found as bellow



Well if you have covered till here then the concept of Implicit DataTemplete is over , assigning the data type to the datatemplete will do the rest of job.Now lets check with the main page of project .

As you can check with the code bellow the main page with 2 controls doesnot have any logic or whatsoever in the xaml

Implicit Data Templates

In this sample the data has been assigned to the both controls on user control load event .The same can be achieved with declarative databinding too.The _employees are getting generated on pageload event with some dummy value.I hope readers will not bother about the source of data Smile.

private void UserControl_Loaded(object sender, RoutedEventArgs e)
    lstEmployees.ItemsSource = _employees;
    cmbEmployees.ItemsSource = _employees;

Lets Run.

Well the the application is ready to run with no efforts any where else.

Implicit Data Templates


The feature gives us a immense flexibility for data presentation ,I don't know how we managed so long  with out it.Your suggestion and comments are always welcome .Let us know how it is going to help you in your application context.

Source Code and Link

Live Link -: Here
Download Source Code-: ImplicitDataTemplate.zip
Make sure that you have downloaded latest Silverlight SDK , if not visit this post for more information on Silverlight 5 Beta

 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend