Search code examples
wpfxamldata-bindingcombobox

WPF ComboBox ItemTemplate binding to a string collection


I have a combobox in wpf which is bound to a List<string>. All works well, but now for some reason I need to bind to an item template. The XAML for the combo box is

<ComboBox ItemsSource="{Binding Tracks}" SelectedItem="{Binding SelectedTrack}">
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock Text="{Binding **WhatShouldBeHere**}"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

If my data source is a custom collection then binding is easy, I should just pass the property name from custom collection, but as the binding source is a list of string, what should the binding property be?.


Solution

  • It should be

    <TextBlock Text="{Binding}"/>
    

    which is equivalent to

    <TextBlock Text="{Binding Path=.}"/>
    

    See the Remarks section on the Binding.Path MSDN page for further details.