I want to autostretch the control to fit a grid by width then set its height to what the grid width is.

Any ideas how I can accomplish this?

有帮助吗?

解决方案

You can achieve that by Binding the Height Property to the ActualWidth Property of the Grid, you can even go further, for example if you want to set the height to be equals to two times of the width, by using a converter :

Code example 1 :

<Grid x:Name="ContentPanel" Grid.Row="1">
        <Button x:Name="Button" Content="Button" Width="200" Height="{Binding ElementName=ContentPanel, Path=ActualWidth}"/>
    </Grid>

Code example 2 :

<Grid x:Name="ContentPanel" Grid.Row="1">
            <Button x:Name="Button" Content="Button" Width="200" Height="{Binding ElementName=ContentPanel, Path=ActualWidth, Converter={StaticResource WidthToHeightConverter}}"/>
        </Grid>
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top