How do you Bind to a ComboBox in a DataTemplate?
I have a listbox that is bound to an observable collection of Audio (custom class). The Audio class has two properties, DisplayText (string) and a property called TarpIds (Observable Collection of Integer). I need to allow the user to change the TarpID in the combo box for each list item displayed and catch the selection change.
I created a DataTemplate that styles the DisplayText property from the Audio object and adds a ComboBox to display the available TarpIDs for this audio (These are dynamic and unique to each Audio). The DisplayText works great, but I can not get the TarpIDs to display in the ComboBox.
Here is what I have so far and thanks for any help. FYI I set the ItemSource at run time that binds the ListUploadAudio to the Observable Collection of Audio.
<Border BorderBrush="Red" Background="WhiteSmoke" CornerRadius="8">
<Border.Resources>
<DataTemplate x:Key="UploadLayout" DataType="Audio">
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding Path=DisplayText}"
FontWeight开发者_C百科="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>
Your ComboBox needs to bind SelectedValue
as well as ItemsSource
.
As far as your ComboBox not binding its items, your code looks right. I would suspect one of the following:
- Misspelling (eg TarpIds vs TarpIDs)
- Improperly defined setting (eg missing getter)
- ListUploadAudio.ItemsSource not being set to expected value
If this doesn't work, I suggest you post your code, specifically the definition of your TarpIds property and the place where you set ListUploadAudio.ItemsSource
As a side note: You don't need the Path= in your bindings unless you are using namespaces.
精彩评论