如何绑定列表<;字符串>;到StackPanel
本文关键字:lt 字符串 gt StackPanel 列表 何绑定 绑定 | 更新日期: 2023-09-27 18:27:31
在我的应用程序中,我有一个string
s的List
,但我不知道如何/不知道如何将此列表绑定到StackPanel
。
我尝试过使用ListBox
,但ListBox
的滚动特性对我的应用程序的用户来说非常不方便。
那么有人知道我如何将string
的List
与StackPanel
结合吗?
我试着在几处房产里乱搞,但什么也没找到。
谢谢你的帮助!
要将可枚举对象绑定到控件并显示它们,可以使用任何ItemsControl
并将对象绑定到ItemsSource
属性。ItemsControl
公开了一个名为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
属性中的内容(默认值基本上是TextBlock
,文本绑定到DataContext
)。对于每个元素,DataContext
将是列表中的项目。例如,如果你有一个string
的列表,你可以做:
<!-- Rest is omitted for succinctness -->
<ItemsControl>
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding .}" FontSize="26" MouseDown="yourEventHandler"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl>
或者,如果您只想更改字体大小,则可以在ItemsControl
本身上使用TextElement.FontSize
依赖属性,或者设置ItemsContainer
:的样式
<ItemsControl TextElement.FontSize="26">
<!-- Rest omitted for succinctness -->
</ItemsControl>
或者:
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="TextElement.FontSize" Value="26"/>
</Style>
</ItemsControl.ItemContainerStyle>
我建议您阅读有关WPF中绑定和项控制的文章/教程,了解有关如何执行各种任务的更多信息,有很多内容需要解释。