.Net Interview Questions and Answers

Search:

What is the difference between control templates and data templates? ( .NET WPF Interview questions)

By : Shiv Prasad Koirala | Mar 27th, 2014 | Views : 4551 | Comments : 0 Bookmark and Share

Download 100 WPF Interview questions with answer from here.


Control templates change the structure of a WPF element while data templates change the way data is displayed but maintain the structure of WPF element.


Below is a visual example of control template where a rectangle shaped button is change in to an ellipse shape.




For that we need to create style using the "" element. In this "" WPF element we can define the structure which we want to apply on the element. Below is the XAML code for the same.


<Style TargetType="Button">

<!--Override all default style -->

<Setter Property="OverridesDefaultStyle" Value="True"/>

<Setter Property="Template">

<Setter.Value>

<ControlTemplate TargetType="Button">

<Grid>

<Ellipse Fill="TemplateBinding Background}"/>

<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>

</Grid>

</ControlTemplate>

</Setter.Value>

</Setter>

</Style>


Later this style can be applied to the button using the style property as shown in below code.


<Button Style="StaticResource mystyle}" />


Data templates have no effect on the structure but they affect data. For example below is list box on which data templates have been applied. You can see the structure of the list box does not change but the number of fields and the way they are placed is changed.



To use data templates we need to use 'ItemTemplate" tag and within the "ItemTemplate" tag we can define the how we want the data to be structured.


<ListBox Margin="26,27,165,51" x:Name="lstb">

<ListBox.ItemTemplate>

<DataTemplate>

<Grid>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="150"></ColumnDefinition>

<ColumnDefinition Width="*"></ColumnDefinition>

</Grid.ColumnDefinitions>

<TextBlock Grid.Column="0" Text="Binding Name}"  FontSize="12" Height="18" />

<TextBlock Grid.Column="1" Text="Binding City}"  FontSize="12" Height="18" />

</Grid>

</DataTemplate>

</ListBox.ItemTemplate>

</ListBox>


Below is a nice WPF interview question video which explains Why WPF is better than winforms ?


 
Rate this Article :
1 Star
2 Stars
3 Stars
4 Stars
5 Stars
1 1 1 2 1
 
 
 
 

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

.NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala

In this article we will explain .NET interview questions 6th edition (Sixth edition) - By Shivprasad Koirala.... Read More

Explain MVC application life cycle?

In this article we will explain MVC application life cycle.For more articles and videos visit us on www.questpond.com... Read More

What is the difference between Stored Procedures and Function (SQL Server interview question with answers)?

In this article we will explain what is the difference between Stored Procedures and Function.For more articles and videos visit us on www.questpond.com... Read More

C# Design pattern interview questions with answers: - How Singleton is different from Static class?

In this article we will explain How Singleton is different from Static class.... Read More

WCF interview questions and answers: - What is the difference between Service endpoint and Client endpoint?

In this article we will explain What is the difference between Service endpoint and Client endpoint.... Read More

Article Categories

Learn in Hindi