#397 – Rich ListBox Content Using Data Binding, part III
September 29, 2011 3 Comments
This post continues the example of displaying information about a series of movies in a ListBox, using data binding.
In previous posts, we created code for a Movie class and filled an ObservableCollection<Movie> with movie data.
To complete this example, we just need the XAML code. Below is the XAML that defines a ListBox that contains the list of movies. We set the ItemTemplate property to a DataTemplate that defines the layout of each item in the list. We then use data binding to bind to properties of the Movie class.
<ListBox ItemsSource="{Binding MovieList}" SnapsToDevicePixels="True"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Image Source="{Binding Image}"/> <StackPanel Orientation="Vertical"> <Label Content="{Binding Title}" FontWeight="Bold"/> <Label Content="{Binding Year}"/> </StackPanel> <Border BorderBrush="Black" BorderThickness="0.5"/> <StackPanel Orientation="Vertical"> <Label Content="Actors:"/> <Label Content="{Binding ActorLead}" Margin="10,0"/> <Label Content="{Binding ActressLead}" Margin="10,0"/> </StackPanel> <Border BorderBrush="Black" BorderThickness="0.5"/> <StackPanel Orientation="Vertical"> <Label Content="Director:"/> <Label Content="{Binding Director}" Margin="10,0"/> </StackPanel> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox>
Pingback: Dew Drop – September 29, 2011 | Alvin Ashcraft's Morning Dew
I like your posts..short and to the point.
Keep up the good work!
Pingback: #424 – Getting Data-Bound Items in a ListBox to Be a Consistent Size « 2,000 Things You Should Know About WPF