Sunday, July 3, 2011

Data Template VS Item Template

For any control we use in WPF there would be two templates that present the data to the user.  One template would be DATA TEMPLATE and the other would be ITEM TEMPLATE.

The main difference between Data template and Item Template is that data template uses the default animation and other settings given by MS for displaying the content, whereas Item Template lets us edit the entire item including the required animations etc.  in simple terms, the scope and flexibility of ITEM TEMPLATE is greater than that of DATA TEMPLATE.

Also, in binding, if we use only data template and not item template the we use code something like this:

ComboBox1.DataContext = CoursesLIST

and if we use item template then source would be defined as:

ComboBox1.ItemsSource = CoursesList

There is a difference in XAML code also, following code is for Data Template XAML Code:

<ComboBox Height="23" Name="CourseslistComboBox1" Width="120">
     <DataTemplate>
            <Label Content="{Binding CourseName}"/>
     </DataTemplate>
</ComboBox>

Whereas, following is the code for the ITEM TEMPLATE:

<ComboBox Height="23" Name="CourseslistComboBox1" Width="120">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <Label Content="{Binding CourseName}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

From the above we can clearly say that Item Template provides more scope of customization when compared to Data Template.

No comments:

Post a Comment