Multicolumn ListBox in WPF

user1156309 picture user1156309 · Jan 18, 2012 · Viewed 50.3k times · Source

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?

I am using .NET WPF. I want to use a ListBox or a ListView.

my window

Answer

Ray picture Ray · Jan 18, 2012

You want a ListView instead:

Something like this:

<ListView ItemsSource="{Binding SourceCollection}">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Test1" DisplayMemberBinding="{Binding Test1}" />
            <GridViewColumn Header="Test2" DisplayMemberBinding="{Binding Test2}" />
            <GridViewColumn Header="Test3" DisplayMemberBinding="{Binding Test3}" />
            <GridViewColumn Header="Button">
                <GridViewColumn.CellTemplate>
                    <DataTemplate>
                        <Button>Button Text</Button>
                    </DataTemplate>
                </GridViewColumn.CellTemplate>
            </GridViewColumn>
        </GridView>
    </ListView.View>
</ListView>