gpt4 book ai didi

c# - 有没有一种简单的方法可以在 WPF 列表框中包装文本?

转载 作者:太空宇宙 更新时间:2023-11-03 18:59:59 24 4
gpt4 key购买 nike

我搜索了一下,找到了一些与Winforms相关的解决方案,甚至有人说在WPF中很难完成,但是那些帖子已经很老了。

如果我有一个标准的 ListBox,声明为:

<ListBox 
x:Name="listBox"
HorizontalAlignment="Left"
Height="240"
Margin="401,68,0,0"
VerticalAlignment="Top"
Width="345"
SelectionChanged="listBox_SelectionChanged"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
Grid.ColumnSpan="2"/>`

以编程方式:

System.ComponentModel.BindingList<string> listItems = new System.ComponentModel.BindingList<string>();
listBox.ItemsSource = listItems;

有没有办法将这些字符串包装在 ListBox 中?

最佳答案

一点也不难:

<ListBox
....
>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<TextBlock
Text="{Binding}"
TextWrapping="Wrap"
/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

关于c# - 有没有一种简单的方法可以在 WPF 列表框中包装文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37621437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com