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.
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 Databut 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
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
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
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.
- Regular Employee
- Hourly Employee(Contractual)
Based on above requirement the class diagram comes up
with somehow as bellow
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.
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
Here in implicit method instead of Key we are going to
add DataType attribute to
sure that you have imported the namespace of the types you are going to use.Here
my emp indicated my namespace for EmployeeClasses.
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.
piece of DataTemplete Xaml wrapped inside a stackPanel and Grid ,can be found as
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
As you can check with the code bellow the main page with 2 controls
doesnot have any logic or whatsoever in the xaml
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 .
private void UserControl_Loaded(object sender, RoutedEventArgs e)
lstEmployees.ItemsSource = _employees;
cmbEmployees.ItemsSource = _employees;
Well the the application is ready to run with
no efforts any where else.
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