
How to Make Data More Presentable Using Data TemplateData
Templates are used to present your data in a pleasing manner so that the user
has a good visual experience with the application. With the data templates you
can make different data look differently so that the user can identify it easily.
The
Dependency controls can bind to data source. The data can be anything like person
name, person object, and product object. The
control can display the data in any format. It can show the name or name and age
or product name, product type etc. This
reporting format can be saved as template in the application resource and can
be reused. Advantages
of Data Templates: The
sample below shows how to use data template to list the details of person in a
list box control <Window
x:Class="SampleDataTemplate.Window1"
<StackPanel Grid.Column="0"
Orientation="Horizontal"> <TextBlock
Grid.Column="1" FontSize="16" Foreground="Blue"
Text="{Binding Address.Street}" /> <StackPanel
Orientation="Horizontal" Grid.Column="2"> <ListBox
Name="personList" SelectionChanged="SelectedPersonChanged"
/> </Grid> The
key points to note in the above example are: DataTemplate
DataType="{x:Type local:Person}" The
Person is a class with properties of person defined. The data template defines
controls for displaying attributes. They also specify the data binding. The
list box is also defined after the resource as, <ListBox
Name="personList" /> In
code behind file the itemsource property is set with collection of person object. personList.ItemsSource
= collection; Once
the ItemSource property is set, the personlist automatically choose the DataTemplate
from the window resource. The
code behind file for the above example is: namespace
SampleDataTemplate
protected virtual void WindowLoaded(object sender, EventArgs e)
for (int i = 0; i < 10; i++)
p.FirstName = "Ramesh" + i;
Address a = new Address();
a.Street = "Dr Street";
p.Address = a;
collection.Add(p);
personList.ItemsSource = collection; Thus
you can use data templates to present data in a more pleasing manner so that it
is easy for the user to read it. Without data templates the data that is displayed
will not be that much readable for the end user.
_______________________________________________________________________ _______________________________________________________________________
FREE
Subscription
Subscribe to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________ |