I need to show a table with headers using ListBox
I searched on the web about how to create this and i found this answer, here is the XAML code i created:
<Grid Grid.Row="1" Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="6*"></RowDefinition>
</Grid.RowDefinitions>
<Grid Grid.Row="0" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="quantityColumn"></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="priceColumn"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="editColumn"></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="deleteColumn"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Foreground="#5a5a5a" TextAlignment="Center" Margin="1" VerticalAlignment="Center">Cantidad</TextBlock>
<TextBlock Grid.Column="1" Foreground="#5a5a5a" TextAlignment="Center" Margin="1" VerticalAlignment="Center">Precio</TextBlock>
<TextBlock Grid.Column="2" Foreground="#5a5a5a" TextAlignment="Center" Margin="1" VerticalAlignment="Center">DescripciĆ³n</TextBlock>
<TextBlock Grid.Column="3" Foreground="#5a5a5a" TextAlignment="Center" Margin="1" VerticalAlignment="Center">Editar</TextBlock>
<TextBlock Grid.Column="4" Foreground="#5a5a5a" TextAlignment="Center" Margin="1" VerticalAlignment="Center">Eliminar</TextBlock>
</Grid>
<ListBox Grid.Row="1" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto" Name="OrderList" Background="Transparent" BorderBrush="{x:Null}" ManipulationBoundaryFeedback="OrderList_ManipulationBoundaryFeedback">
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Focusable" Value="False"/>
<EventSetter Event="RequestBringIntoView" Handler="ListBoxItem_RequestBringIntoView"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<Grid ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="quantityColumn"></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="priceColumn"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="editColumn"></ColumnDefinition>
<ColumnDefinition SharedSizeGroup="deleteColumn"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Foreground="White" TextAlignment="Center" Margin="7">Cantidad</TextBlock>
<TextBlock Grid.Column="1" Foreground="White" TextAlignment="Center" Margin="7">Precio</TextBlock>
<TextBlock Grid.Column="2" Foreground="White" TextAlignment="Center" Margin="7">DescripciĆ³n</TextBlock>
<TextBlock Grid.Column="3" Foreground="White" TextAlignment="Center" Margin="7">Editar</TextBlock>
<TextBlock Grid.Column="4" Foreground="White" TextAlignment="Center" Margin="7">Eliminar</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
And here is the c# code i created to test and populate the ListBox
List<OrderItem> orders = new List<OrderItem>();
orders.Add(new OrderItem() { a = "wash", b = "b" });
orders.Add(new OrderItem() { a = "the", b = "c" });
orders.Add(new OrderItem() { a = "car", b = "d" });
orders.Add(new OrderItem() { a = "wash", b = "b" });
OrderList.ItemsSource = orders;
Since the ListBox
only shows 4 rows, it doesnt have to set a scrollbar, and the headers are almost 100% aligned with the table columns:
The problem worsens when i add more rows to the ListBox
and the scrollbar appears:
As you can see the offset of each column with its header its too big. How can i tell the Grid.IsSharedSizeScope
to be aware of the width of the scrollbar and to consider it to the headers width? What i need it's to have the table columns 100% aligned with the table headers.