Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Full width DataGridColumn on DataGrid's width

Tags:

c#

wpf

datagrid

I have a WPF Window with a DataGrid inside a Grid like this:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" SizeToContent="WidthAndHeight" WindowStyle="ToolWindow" ResizeMode="NoResize">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>

        <TextBlock FontSize="20">Some header with some pretty longish text</TextBlock>

        <DataGrid Grid.Row="1" Height="200">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Column1" Width="*"/>
            </DataGrid.Columns>
        </DataGrid>

    </Grid>
</Window>

I'd like the DataGrid's width to be at least the width of the TextBlock, which in this case is, because of the Grid's ColumnDefinition's width set to Auto.

But somehow, the width of the DataGrid's Column is at a minimum (and can't even be resized), while I expect it's width to be filled. Currently it looks like this:
enter image description here

Note that the window has to be a ToolWindow and it's SizeToContent set to WidthAndHeight.

like image 308
QuantumHive Avatar asked Jul 14 '26 10:07

QuantumHive


2 Answers

This works at runtime but does not (always) seem to show up right in the designer.

  1. Surround the DataGrid in some wrapper (another Grid is fine)
  2. Bind the DataGrid's with to the ActualWidth of the wrapper either using relative source or element name:

    a. Width="{Binding Path=ActualWidth, ElementName=WrapperGrid}"

    b. Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}}"

If you don't have the SizeToContent="WidthAndHeight" option it does not show up right in the designer:

enter image description here

But, no matter what after you run it, the width is proper:

With SizeToContent="WidthAndHeight":

enter image description here

Without SizeToContent="WidthAndHeight":

enter image description here

Full code below:

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow"
        SizeToContent="WidthAndHeight"
        WindowStyle="ToolWindow"
        ResizeMode="NoResize">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition
                Width="Auto" />
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition
                Height="Auto" />
            <RowDefinition
                Height="Auto" />
        </Grid.RowDefinitions>

        <TextBlock
            x:Name="Tb1"
            FontSize="20">Some header with some pretty longish text</TextBlock>

        <Grid
            Grid.Row="1"
            Grid.Column="0">

            <DataGrid
                Grid.Row="1"
                Height="200"
                Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=Grid}}"
                AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn
                        Header="Column1"
                        Width="*" />
                </DataGrid.Columns>
            </DataGrid>
        </Grid>

    </Grid>
</Window>
like image 118
chancea Avatar answered Jul 16 '26 04:07

chancea


Get rid of

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
    </Grid.ColumnDefinitions>

Or set Width to * instead of Auto.

like image 23
Mathiesen Avatar answered Jul 16 '26 04:07

Mathiesen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!