Introduction
This blog builds on the previous one. That earlier blog uses an ObjectDataProvider to access a collection of DrinkProduct objects and displays the ProductName property.
It’ll be a rare day when you only need to access a single property of a collection like this. Very often you’ll want to display some kind of key list, like the ProductName, first and then show users further details as they select individual items from the ListBox. So that’s what we’ll look into here.
I’m going to use a very basic approach, putting everything inside a parent StackPanel. So we can see what we’re aiming for, here’s the finished window:
You can see that the Instant Tea item is selected and that the correct Product ID and current stock figures for this item are shown. As the user changes the selection, so the details on the right hand side will change to reflect the current choice.
The Document Outline Window
Because the XAML might take a bit of studying to see what goes where, one tool that you’ll often find useful is the Document Outline in the IDE. You can select this with Ctrl + Alt + T, or find it in the View menu. It is very useful for getting an overview of the structure of the Window:
In this example, we’re interested in the first StackPanel and everything below that in the displayed list of controls (actually, to be totally accurate, they’re ‘elements’ in WPF, but old habits die hard!).
The first StackPanel, which is named PanelOuter contains two children – a ListBox named lstDrinks, and another StackPanel, which is named PanelRight. PanelRight also has children – in this case two labels and two text blocks. So, essentially, the outer StackPanel is split vertically, has a ListBox in the left half and a StackPanel with its own content in the right half. The Document Outline is often most useful for working out the nesting pattern of elements when things get complicated.
Markup for the Window
Here’s all the markup for the window. I’ll pick through it in the next few sections and highlight the parts you really need to home in on.
1 <Window x:Class="MainWindow"
2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4 xmlns:local="clr-namespace:ObjectDataProviderBlogDemo"
5 Title="Drink Product Stocks" Height="350" Width="375">
6
7 <Window.Resources>
8 <ObjectDataProvider x:Key="DrinksInfo"
9 ObjectType="{x:Type local:DrinkProduct}"
10 MethodName="StockCheck"></ObjectDataProvider>
11 </Window.Resources>
12
13 <StackPanel Orientation="Horizontal" Name="PanelOuter"
14 DataContext="{Binding Source={StaticResource DrinksInfo}}">
15
16 <ListBox Name="lstDrinks" Margin="2,4,14,2" Padding="1,0,16,1"
17 ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"/>
18
19 <StackPanel Orientation="Vertical" Margin="15,3,3,3"
20 Name="PanelRight">
21 <Label Content="Product ID" Foreground="MediumBlue" />
22 <TextBlock Margin="3" Padding="3" HorizontalAlignment="Center"
23 Text="{Binding Path=ProductID}"></TextBlock>
24 <Label Content="Quantity Available" Foreground="MediumBlue"
25 Margin="3,45,3,3" />
26 <TextBlock Margin="3" Padding="3" HorizontalAlignment="Center"
27 Text="{Binding Path=Quantity}"></TextBlock>
28 </StackPanel>
29 </StackPanel>
30
31 </Window>
32
Markup for the Window Opening Tag and Window Resources
This is exactly the same as I used in the previous blog item, so I won’t go over it again here.
Data Binding Markup for the Outer StackPanel
Bearing in mind that several of the elements inside the outer StackPanel need to be bound to the data source, it makes sense to set the data binding on this outer item. Here’s the opening tag for that StackPanel:
<StackPanel Orientation="Horizontal" Name="PanelOuter"
DataContext="{Binding Source={StaticResource DrinksInfo}}">
This time I’ve created a DataContext that ensures that the DrinkProduct data from the ObjectDataProvider is available throughout the StackPanel.
Data Binding Markup for the ListBox
Because the ListBox is a child of the StackPanel, there’s no longer any need to reference that DrinksInfo StaticResource directly. But the ItemsSource still needs to know where its data is coming from, so a simple Binding which links to the default parent binding is used:
<ListBox Name="lstDrinks" Margin="2,4,14,2" Padding="1,0,16,1"
ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True"/>
The other addition to the ListBox markup is the IsSynchronizedWithCurrentItem property. This is an important feature in these kinds of scenarios where the user clicks on a ListBox item and this is meant to update some other bound control(s). It’s a very long-winded property name, but simply all it means is that all other elements that are bound to the same source listen for this, and are automatically updated when any changes happen.
Markup for the Right Hand Panel
There are several ways of creating the same finished layout. This one is reasonably straightforward. The labels have static content that describe what’s in the TextBlocks below them. If you wanted to have the label and the TextBlock aligned horizontally, you could insert further sub StackPanels inside PanelRight.
I don’t know of a way to concatenate the static text and the bound result in a single control by using the XAML markup in the window (in other words to have something like “Quantity: 123” inside a single TextBlock). But if you’re determined to get this result, you could use a ValueConverter.
The TextBlocks are both bound to the same data source – again channelled through the DataContext of the parent PanelOuter StackPanel. Each of them has a path to a different field. In the case of the first TextBlock, that path points to the ProductID field:
<TextBlock Margin="3" Padding="3" HorizontalAlignment="Center"
Text="{Binding Path=ProductID}"></TextBlock>
Summary
And that’s pretty much all there is to it. I’ve attached the sample project if you want to experiment.

Apple has a long history of using slot-loading disc drives rather than the tray-loading drives used widely across the rest of the industry. If you want to eject the disc, there are currently two ways of doing this in OS X that most users know of, but there’s actually a hidden app that you may not know about.
