Multicolumn ListBox in WPF(WPF 中的多列列表框)
问题描述
我有 3 个 TextBoxes 和 1 个 Button 并且想将每个 TextBoxes 数据输入到 ListBox 在单独的列中.
I have 3 TextBoxes and 1 Button and want to enter each of the the TextBoxes data into a ListBox in separate columns.
我知道如何将数据输入一列:
I know how to enter data into one column:
listbox1.Items.Add(TextBox1.text);
但是如何将数据输入到多个列中?
but how can I enter the data into multiple columns?
我正在使用 .NET WPF.我想使用 ListBox 或 ListView.
I am using .NET WPF. I want to use a ListBox or a ListView.
我的窗口
推荐答案
Ray 正确指出,ListView 会做工作.但是,如果您坚持使用和/或想要使用 ListBox,您也可以使用带有 Grid 的 ItemTemplate 并设置 Grid.IsSharedSizeScope 属性.例如:
As Ray correctly points out, ListView will do the job. However if you're stuck with and/or want to use ListBox, you can also use an ItemTemplate with Grid and set the Grid.IsSharedSizeScope property on the ListBox itself. For example:
<ListBox ItemsSource="{Binding DataSource}" Grid.IsSharedSizeScope="True">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="Column1"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="Column2"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="Column3"/>
</Grid.ColumnDefinitions>
<!-- Assumes MVVM and you wish to bind to properties and commands -->
<TextBlock Grid.Column="0" Text="{Binding ColumnOneText}"/>
<TextBlock Grid.Column="1" Text="{Binding ColumnTwoText}"/>
<TextBlock Grid.Column="2" Text="{Binding ColumnThreeText}"/>
<Button Content="ClickMe" Command="{Binding ButtonExecutionCommand}"/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
这是一个巧妙的技巧,也可以应用于您有多个 Grid 实例的其他情况(即:根据这种情况,每个 ListBoxItem 一个)并希望共享一列.
This is a neat trick that can also be applied to other cases where you have multiple instances of Grid (ie: one per ListBoxItem as per this case) and want to share a column.
这篇关于WPF 中的多列列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:WPF 中的多列列表框
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
