Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wpf Listbox giving columns a header

Tags:

c#

wpf

xaml

listbox

I have the following markup (xaml):

<ListBox Name="lbEurInsuredType" HorizontalContentAlignment="Stretch">
            <ListBox.ItemTemplate>
               <DataTemplate>
                    <Grid Margin="0,2">
                        <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="30"></ColumnDefinition><ColumnDefinition Width="2"></ColumnDefinition>
                            <ColumnDefinition Width="30"></ColumnDefinition>
                   </Grid.ColumnDefinitions>
                   <TextBlock Text="{Binding Title}"></TextBlock>
                   <TextBox Text="{Binding Uw}" Grid.Column="1"></TextBox>
                   <TextBox Text="{Binding Partner}" Grid.Column="3"></TextBox>
                </Grid>
            </DataTemplate></ListBox.ItemTemplate>

        </ListBox>

This all looks ok, but now above column 1 and 3 I want to place a header. Can anyone show me how I add headers to my two columns.

like image 865
bilpor Avatar asked Sep 08 '15 08:09

bilpor


2 Answers

I recommend that you use a ListView instead which more appropriate to you case, you could use a GridView inside and define the columns that you need then restyle them much more easily

 <ListView Name="lbEurInsuredType" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Items}">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Title}"></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        <GridViewColumn.Header>
                            <TextBlock Text="Column 1"></TextBlock>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Uw}"></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>                           
                    </GridViewColumn>
                    <GridViewColumn>
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Partner}"></TextBlock>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                        <GridViewColumn.Header>
                            <TextBlock Text="Column 3"></TextBlock>
                        </GridViewColumn.Header>
                    </GridViewColumn>
                </GridView.Columns>
            </GridView>
        </ListView.View>                      
    </ListView>
like image 115
SamTh3D3v Avatar answered Sep 19 '22 20:09

SamTh3D3v


Listview is surely the best option, but, if you need to use a listbox you could modify the template of the listbox in this way.

   <ListBox Name="lbEurInsuredType" HorizontalContentAlignment="Stretch">
        <ListBox.Template>
            <ControlTemplate>
                <DockPanel LastChildFill="True">
                    <Grid DockPanel.Dock="Top" Height="30">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="30"></ColumnDefinition>
                            <ColumnDefinition Width="2"></ColumnDefinition>
                            <ColumnDefinition Width="30"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Label Grid.Column="0">Column 1</Label>
                        <Label Grid.Column="1">Column 2</Label>
                        <Label Grid.Column="2">Column 3</Label>
                        <Label Grid.Column="3">Column 4</Label>
                    </Grid>
                    <ItemsPresenter></ItemsPresenter>
                </DockPanel>
            </ControlTemplate>
        </ListBox.Template>
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid Margin="0,2">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"></ColumnDefinition>
                        <ColumnDefinition Width="30"></ColumnDefinition>
                        <ColumnDefinition Width="2"></ColumnDefinition>
                        <ColumnDefinition Width="30"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding Title}"></TextBlock>
                    <TextBox Text="{Binding Uw}" Grid.Column="1"></TextBox>
                    <TextBox Text="{Binding Partner}" Grid.Column="3"></TextBox>
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
like image 28
Mattia Magosso Avatar answered Sep 17 '22 20:09

Mattia Magosso