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

偶尔善良 提交于 2019-12-04 17:11:34

问题


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?


回答1:


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




回答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.


来源:https://stackoverflow.com/questions/1393423/how-do-i-bind-a-liststring-to-an-itemscontrol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!