WPF Follow Parent’s Size 1

2014-05-03


There are different ways to get child size follows parent’s size, here I collect 3 of them first:

1: Using ElementName:

<…. Parent container > (note: StackPanel not work maybe? not sure here)  
<TextBox Width="{Binding ElementName=ParentContainer, Path=ActualWidth}"
    Text="Text" HorizontalAlignment="Left" MaxWidth="200" />
</…. Parent container >

2: Using DataTemplate:

Width="{Binding ActualWidth,RelativeSource={RelativeSource FindAncestor, 
AncestorType={x:Type ScrollContentPresenter}}}"

3: Use Grid:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*" MaxWidth="100"/>
    </Grid.ColumnDefinitions>

    <TextBox Text="Text" />
</Grid>

Anyway, you have to know different case get different result, not guarantee above methods work for your case.