WPF 中的多列列表框

9

本文介绍了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.我想使用 ListBoxListView.

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 中的多列列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14

具有未知类型的 CreateDelegate
CreateDelegate with unknown types(具有未知类型的 CreateDelegate)...
2023-11-11 C#/.NET开发问题
5

Func&lt;T&gt;.BeginInvoke 使用线程池吗?
Does Funclt;Tgt;.BeginInvoke use the ThreadPool?(Funclt;Tgt;.BeginInvoke 使用线程池吗?)...
2023-11-11 C#/.NET开发问题
6

如何为具有空目标的实例方法创建委托?
How to create a delegate to an instance method with a null target?(如何为具有空目标的实例方法创建委托?)...
2023-11-11 C#/.NET开发问题
6