WPF 中的多列列表框 [英] Multicolumn ListBox in WPF

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆