18

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

2 Answers2

42

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

Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047
  • 12
    FYI, '.' works either with or without 'Path='. Blank is implicitly interpreted as a '.'. So you can write {Binding}, {Binding .}, or {Binding Path=.} Note that {Binding Path=} won't work - you'll get a XAML compiler error. – Kent Boogaart Sep 08 '09 at 12:10
  • 4
    haha. Question is asked at 11:15 and answered by exact same person just a minute later. Then answer is accepted :) well done. – avepr Mar 20 '15 at 04:31
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.
The Boondoggler
  • 105
  • 1
  • 8