How do I bind a List<string> to an ItemsControl?

18,694

Solution 1

let me answer this, it's just {Binding}.

Solution 2

An easier way to accomplish the same thing is to simply use:

<ItemsControl ItemsSource="{Binding PropertyNames}"/>

By default, this will create a vertical StackPanel and add each element in its own TextBlock. According to MSDN, this works for any of the following:

  • A string.
  • A DateTime object.
  • A UIElement object.
  • A Panel control that contains an Ellipse and a TextBlock.
Share:
18,694
Angry Dan
Author by

Angry Dan

web/software developer, .NET, C#, WPF, PHP, software trainer, English teacher, have philosophy degree, love languages, run marathons my tweets: http://www.twitter.com/edward_tanguay my runs: http://www.tanguay.info/run my code: http://www.tanguay.info/web my publications: PHP 5.3 training video (8 hours, video2brain) my projects: http://www.tanguay.info

Updated on June 06, 2022

Comments

  • Angry Dan
    Angry Dan almost 2 years

    In my presenter I have this property:

    public List<string> PropertyNames { get; set; }
    

    And I want to list out the names with a ItemsControl/DataTemplate like this:

    <ItemsControl ItemsSource="{Binding PropertyNames}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Value}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
    

    Since the generic list doesn't have named properties, how do I reference the value in my Binding statement?