在我的应用程序中,我有一个Listof strings,但是我不知道如何/不知道如何将此列表绑定到一个StackPanel.
我试过使用 aListBox但是滚动的性质对ListBox我的应用程序的用户来说非常不方便。
那么有谁知道我如何将 a Listof strings 绑定到 a StackPanel?
我试过弄乱几个属性,但没有找到任何东西。
谢谢你的帮助!
在我的应用程序中,我有一个Listof strings,但是我不知道如何/不知道如何将此列表绑定到一个StackPanel.
我试过使用 aListBox但是滚动的性质对ListBox我的应用程序的用户来说非常不方便。
那么有谁知道我如何将 a Listof strings 绑定到 a StackPanel?
我试过弄乱几个属性,但没有找到任何东西。
谢谢你的帮助!
要将枚举绑定到控件并显示它们,您可以使用任何一个ItemsControl并将您的对象绑定到ItemsSource属性。ItemsControls 公开了一个名为的属性,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>
编辑:
至于您的评论,任何内容都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>
或者,如果您只想更改字体大小,则可以使用自身的TextElement.FontSizeDependency 属性ItemsControl或设置以下样式ItemsContainer:
<ItemsControl TextElement.FontSize="26">
<!-- Rest omitted for succinctness -->
</ItemsControl>
或者:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="TextElement.FontSize" Value="26"/>
</Style>
</ItemsControl.ItemContainerStyle>
我建议您阅读有关 WPF 中的绑定和项目控制的文章/教程,以获取有关如何执行各种任务的更多信息,有很多要解释的。