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 中的多列列表框
基础教程推荐
- 获取C#保存对话框的文件路径 2022-01-01
- 从 C# 控制相机设备 2022-01-01
- Mono https webrequest 失败并显示“身份验证或解密失败" 2022-01-01
- SonarQube C# 分析失败“不是指针的有效行偏移" 2022-01-01
- 更新 Visual Studio 中的 DataSet 结构以匹配新的 SQL 数据库结构 2022-01-01
- C# 9 新特性——record的相关总结 2023-04-03
- 在 VB6 或经典 ASP 中使用 .NET 2022-01-01
- 如果条件可以为空 2022-01-01
- 将数据集转换为列表 2022-01-01
- 重新排序 WPF TabControl 中的选项卡 2022-01-01
