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:

Note that the window has to be a ToolWindow and it's SizeToContent set to WidthAndHeight.
This works at runtime but does not (always) seem to show up right in the designer.
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:

But, no matter what after you run it, the width is proper:
With SizeToContent="WidthAndHeight":

Without SizeToContent="WidthAndHeight":

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>
Get rid of
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
Or set Width to * instead of Auto.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With