Consider a scenerio , we have a listbox and some process where we have a tab navigation. On select of a tab, pass some inteeger value to a hidden textbox in the xaml . now based on the tab selected, we can switch the same listbox with different data template. This will avoid creating 2 listboxes in Silverlight 3 application.
the below code will explain in detail :
<ListBox x:Name="listbox1">
<ListBox.ItemTemplate>
<DataTemplate>
<ContentControl Content="{Binding}" Loaded="ContentControl_Loaded"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<DataTemplate x:Key="datatemplate01">
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<TextBlock Text="{Binding Dept}"/>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="datatemplate02">
<StackPanel>
<TextBlock Foreground="Blue" Text="{Binding Name}"/>
<TextBlock Foreground="Blue" Text="{Binding Address}"/>
</StackPanel>
</DataTemplate>
private void ContentControl_Loaded(object sender, RoutedEventArgs e)
{
ContentControl mycontentcontrol= (ContentControl)sender;
Data emp= (Data) mycontentcontrol.DataContext;
if (hiddentxt.Text ==1)
{
mycontentcontrol.ContentTemplate = (DataTemplate)this.Resources["datatemplate01"];
}
else if(hiddentxt.Text ==2)
{
mycontentcontrol.ContentTemplate = (DataTemplate)this.Resources["datatemplate01"];
}
}