0

Context: I'd like to create a DataGrid with one row, which should take the whole place. As I learned, I Need to set the Width of the DataGrid to assure this code works:

        <StackPanel x:Name="stackMails" Orientation="Horizontal">
            <TextBlock Text="Personal E-Mails:" />
            <DataGrid x:Name="dgPersonalMail" VerticalScrollBarVisibility="Auto" ItemsSource="{Binding SupportConfiguration.PersonalMailAddresses}" CanUserAddRows="True" CanUserDeleteRows="True" CanUserResizeRows="False" AutoGenerateColumns="False">
                <DataGrid.Columns>
                    <DataGridTextColumn Header="E-Mail" Binding="{Binding}" Width="*"  />
                </DataGrid.Columns>
            </DataGrid>

Since I'd like to make the size of the DataGrid dynamic, I bound the width of the DataGrid to the ActualWidth of the Stackpanel:

<Style TargetType="DataGrid">
    <Setter Property="Width">
        <Setter.Value>
            <Binding Path="ActualWidth" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=StackPanel, AncestorLevel=1}"  />
        </Setter.Value>
    </Setter>

    <Setter Property="MaxWidth" Value="400" />
    <Setter Property="MaxHeight" Value="350" />
    <Setter Property="MinHeight" Value="150" />
</Style>

This is a solution I found on SO as well, seeing here: How do I make XAML DataGridColumns fill the entire DataGrid? (Binding it on the Window has the same behavior.)

But now the Problems start: Seeing this Thread WPF What happens when a child's width is bound to a parent's actualwidth . This is exactly the behavior I get: Without MaxWidth, the size of the DataGrid grows forever.

I kindahow see, why it grows (Updating child Updating parent etc. etc.). But I guess I'm not the first Person who'd like to make this work, so I guess I'm doing something wrong here?

Community
  • 1
  • 1
Matthias Müller
  • 3,336
  • 3
  • 33
  • 65

0 Answers0