[英]How do I bind a List<string> to an ItemsControl?
在我的演示者中,我具有以下屬性:
public List<string> PropertyNames { get; set; }
我想用ItemsItem / DataTemplate列出這些名稱:
<ItemsControl ItemsSource="{Binding PropertyNames}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Value}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
由於通用列表沒有命名屬性,如何在Binding語句中引用該值 ?
我來回答,這只是{Binding}
。
完成同一件事的一種更簡單的方法是簡單地使用:
<ItemsControl ItemsSource="{Binding PropertyNames}"/>
默認情況下,這將創建一個垂直的StackPanel,並將每個元素添加到其自己的TextBlock中。 根據MSDN ,這適用於以下任何一種:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.