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?

2
  • why dont you use a Grid as a container? Jan 8, 2015 at 12:19
  • Would that solve the problem? I thought this "Child -> Parent -> Child" behavior would happen at every Content Control Jan 9, 2015 at 7:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.