How do you bind to the ComboBox in the DataTemplate?
I have a list associated with an Audio observable collection (custom class). The Audio class has two properties: DisplayText (string) and TarpIds (Observable Collection of Integer) property. I need to allow the user to change the TarpID in the combo box for each displayed list item and catch the selection change.
I created a DataTemplate that styles the DisplayText property from an Audio object and adds a ComboBox to display the available TarpIDs for that audio (they are dynamic and unique to each audio). DisplayText works fine, but I can't get the TarpIDs to display in the ComboBox.
Here's what I have so far and thanks for any help. FYI I have set an ItemSource at runtime that binds the ListUploadAudio to an observable audio collection.
<Border BorderBrush="Red" Background="WhiteSmoke" CornerRadius="8">
<Border.Resources>
<DataTemplate x:Key="UploadLayout" DataType="Audio">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=DisplayText}"
FontWeight="Bold" Foreground="Blue">
</TextBlock>
<ComboBox x:Name="ListBoxTarpIDs"
ItemsSource="{Binding Path=TarpIds}">
</ComboBox>
</StackPanel>
</DataTemplate>
</Border.Resources>
<ListBox x:Name="ListUploadAudio" BorderBrush="Transparent"
Background="Transparent" Width="230" Margin="10"
Height="200" IsSynchronizedWithCurrentItem="True" SelectionMode="Multiple"
ItemTemplate="{DynamicResource UploadLayout}">
</ListBox>
</Border>
a source to share
For your ComboBox you need to bind SelectedValue
as well ItemsSource
.
As for your ComboBox not linking its items, your code looks correct. I suspect one of the following:
- Error (e.g. TarpIds vs TarpIDs)
- Incorrectly defined setting (e.g. missing getter)
- ListUploadAudio.ItemsSource not configured to expected value
If that doesn't work, I suggest you post your code, specifically the definition of your TarpIds property and the location where you set the ListUploadAudio.ItemsSource
As a side note: you don't need Path = in your bindings unless you are using namespaces.
a source to share