WPF - Very basic ListBox.ItemTemplate Question
ItemTemplate
wont work when you put ListBoxItem
directly as items. General concept is you databind a CRL collection to the ListBox.ItemsSource
and then specify the ItemTemplate
. Check the below code.
<Grid x:Name="LayoutRoot"> <ListBox x:Name="TestList" SelectionMode="Multiple"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel> <CheckBox Content="Check this checkbox!"/> <TextBlock Text="{Binding}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> <ListBox.Items> <sys:String>Bob</sys:String> <sys:String>Jim</sys:String> <sys:String>Dave</sys:String> <sys:String>Larry</sys:String> <sys:String>Tom</sys:String> </ListBox.Items> </ListBox> </Grid>
where sys
is xmlns:sys="clr-namespace:System;assembly=mscorlib"
In this way, there are 5 ListBoxItems
getting generated in the background and added to the ListBox
.
You can use ItemContainerStyle instead of ItemTemplate if you want to add ListBoxItems directly to the ListBox.
Doing so, however, is only recommended when you need unique characteristics on a per item level.
If you are planning on all the items looking the same or making a dynamic list using ItemsSource, I would recommend you add strings (or another custom object) to your list and use ItemTemplate to display your items. (see Jobi Joy's answer)
Here's an example using ItemContainerStyle:
<ListBox x:Name="TestList" SelectionMode="Multiple"> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <StackPanel> <CheckBox Content="Check this checkbox!" /> <TextBlock Text="{TemplateBinding Content}" /> </StackPanel> </ControlTemplate> </Setter.Value> </Setter> </Style> </ListBox.ItemContainerStyle> <ListBox.Items> <ListBoxItem>Bob</ListBoxItem> <ListBoxItem>Jim</ListBoxItem> <ListBoxItem>Dave</ListBoxItem> <ListBoxItem>Larry</ListBoxItem> <ListBoxItem>Tom</ListBoxItem> </ListBox.Items> </ListBox>
For some reason DataTemplate can still be ignored if the ListBox is populated using ItemsSource e.g:
<ListBox Name="Test" x:FieldModifier="public" ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged}"> <ListBox.ItemTemplate> <DataTemplate> <TextBox Text="{Binding Text, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Note that this is bound to an ObservableCollection containing objects (TextAdapter : INotifyPropertyChanged) with one property: string Text {...}