Multicolumn ListBox in WPF

2019-01-23 04:09发布

问题:

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

回答1:

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>


回答2:

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>

This is a neat trick which 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.

Best regards,



回答3:

use groupbox and docpanel

<GroupBox Width="250"
                          Margin="10,0,0,0"                          
                          FontSize="20"
                          Header="کسورات"
                          Style="{StaticResource Gb}">
                    <ListBox Margin="0" AlternationCount="2">
                        <ListBoxItem>
                            <Border Margin="0"
                                    BorderBrush="#ddd"
                                    BorderThickness="0,1,0,1">
                                <DockPanel Background="#f9f9f9" LastChildFill="True">


                                    <controls2:TimeEditBox DockPanel.Dock="Right"
                                                           FontSize="13"
                                                           Mask="00:00"
                                                           Text="00:00"
                                                           TextAlignment="Center"
                                                           controls2:TextBoxMaskBehavior.Mask="Integer" />
                                    <TextBox Width="50"
                                             Margin="0,0,5,0"
                                             DockPanel.Dock="Right"
                                             FontSize="13"
                                             Text="122"
                                             TextAlignment="Center"
                                             controls2:TextBoxMaskBehavior.Mask="Integer" />
                                    <TextBlock VerticalAlignment="Center"
                                               FontSize="13"
                                               Text="غیبت"
                                               TextAlignment="Center" />
                                </DockPanel>
                            </Border>
                        </ListBoxItem>
                      **</ListBox>**
                </GroupBox>