Blog Archive

Monday, May 19, 2008

Control template vs. Data Template

In the following Example, both Control Template and Data Template will fill ListBox using List Data. But Control Template disable ListBox Selection.

<Window.Resources>
<!--<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type ListBoxItem}"> <TextBlock Text="{Binding Path=Symbol}"></TextBlock> </ControlTemplate>
</Setter.Value>
</Setter>
</Style>-->
<ObjectDataProvider x:Key="Stocks" ObjectType="{x:Type data:Stocks}" />
<DataTemplate x:Key="ShowStock">
<TextBlock Text="{Binding Path=Symbol}">

</DataTemplate>
</Window.Resources>
<Grid Height="182" Width="240"> <Grid.RowDefinitions> <RowDefinition> <RowDefinition> </Grid.RowDefinitions>
<ListBox ItemsSource="{Binding Source={StaticResource Stocks}}" Name="StockList1" Grid.Row="0" ItemTemplate="{Binding Source={StaticResource ShowStock}}" />
<ListBox ItemsSource="{Binding Source={StaticResource Stocks}}" Name="StockList2" Grid.Row="1" />
</Grid>

No comments: