I have made 2 data templates and using it as a resource,i am applying it to a listbox,i am able to apply only a single data template on the listbox,here is the code for both data template
<Window.Resources>
<DataTemplate x:Key="template1">
<Canvas Height="40" Width="850">
<Label Height="30" Width="170" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
<TextBox Height="30" Width="120" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
<Label Canvas.Left="420" Canvas.Top="5">$</Label>
</Canvas>
</DataTemplate>
<DataTemplate x:Key="template2">
<Canvas Height="40" Width="850">
<Label Height="30" Width="200" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
<TextBox Height="30" Width="200" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
<Label Canvas.Left="420" Canvas.Top="5">$</Label>
</Canvas>
</DataTemplate>
</Window.Resources>
and the code for listbox
<TabItem>
<Canvas Height="700" Width="850">
<ListBox x:Name="listBox" Height="700" Width="850" ItemTemplate="{StaticResource template1}">
</ListBox>
</Canvas>
</TabItem>
how can i apply both the data templates to the listbox,presently only "template1" is getting applied,how can "template2" be applied or if there are many data templates in future.,is there any way??,thanx
You can use multiple DataTemplates using DataTemplateSelector and Datatrigger but you havent explain any condition(selected item,unselected item,index,change foreground and fontsize etc.)
It seems that you want template for displaying purpose/UI purpose only then you need to change template for each listboxitem like below
<Window.Resources>
<ControlTemplate x:Key="template1">
<Canvas Height="40" Width="850">
<Label Height="30" Width="170" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
<TextBox Height="30" Width="120" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
<Label Canvas.Left="420" Canvas.Top="5">$</Label>
</Canvas>
</ControlTemplate>
<ControlTemplate x:Key="template2">
<Canvas Height="40" Width="850">
<Label Height="30" Width="200" Canvas.Top="5" Canvas.Left="80" Background="LightGray"></Label>
<TextBox Height="30" Width="200" Canvas.Top="5" Canvas.Left="300" Background="AliceBlue"></TextBox>
<Label Canvas.Left="420" Canvas.Top="5">$</Label>
</Canvas>
</ControlTemplate>
</Window.Resources>
<Grid>
<TabControl>
<TabItem>
<Canvas Height="700" Width="850">
<ListBox x:Name="listBox" Height="700" Width="850">
<ListBoxItem Template="{StaticResource template1}"></ListBoxItem>
<ListBoxItem Template="{StaticResource template2}"></ListBoxItem>
</ListBox>
</Canvas>
</TabItem>
</TabControl>
</Grid>