Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 813
Edward Tanguay Avatar asked Sep 08 '09 11:09

Edward Tanguay


2 Answers

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

like image 160
Edward Tanguay Avatar answered Oct 14 '22 12:10

Edward Tanguay


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.
like image 1
The Boondoggler Avatar answered Oct 14 '22 12:10

The Boondoggler