New*_*bie -2 c# binding list windows-phone-7 windows-phone-8
在我的应用程序有一个List的stringS,但我不能找出如何/不知道如何向此列表绑定到StackPanel。
我试过使用 aListBox但它的滚动特性ListBox对我的应用程序的用户来说非常不方便。
那么有谁知道我如何将 a Listof strings绑定到 a StackPanel?
我试过弄乱几个属性,但没有找到任何东西。
谢谢你的帮助!
要将枚举绑定到控件并显示它们,您可以使用ItemsControl和 将对象绑定到ItemsSource属性。该ItemsControl系统的expose一个名为属性ItemsPanel中,你可以进一步修改,以改变该项目的容器。StackPanel是大多数的默认设置。
<ItemsControl ItemsSource="{Binding NewbieList}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<!-- The default for an ItemsControl is a StackPanel with a vertical orientation -->
<StackPanel Orientation="Horizontal"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
编辑:
至于您的评论,ItemsSource将“输出”ItemTemplate属性中的任何内容(默认值基本上是 a TextBlock,文本绑定到DataContext)。对于每个元素,DataContext将是列表中的项目。string例如,如果您有一个 列表 ,您可以执行以下操作:
<!-- Rest is omitted for succinctness -->
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding .}" FontSize="26" MouseDown="yourEventHandler"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl>
Run Code Online (Sandbox Code Playgroud)
或者,如果您只想更改字体大小,您可以TextElement.FontSize在其ItemsControl本身上使用Dependency 属性或设置样式ItemsContainer:
<ItemsControl TextElement.FontSize="26">
<!-- Rest omitted for succinctness -->
</ItemsControl>
Run Code Online (Sandbox Code Playgroud)
或者:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="TextElement.FontSize" Value="26"/>
</Style>
</ItemsControl.ItemContainerStyle>
Run Code Online (Sandbox Code Playgroud)
我建议您阅读有关 WPF 中绑定和项目控制的文章/教程,以获取有关如何执行各种任务的更多信息,有很多要解释的。