自动伸展xaml控制宽度,并根据其宽度设置高度?

时间:2014-02-09 11:46:54

标签: xaml layout height width stretch

我希望自动伸展控件以按宽度调整网格,然后将其高度设置为网格宽度。

我有什么想法可以做到这一点?

1 个答案:

答案 0 :(得分:1)

您可以通过将高度属性绑定到网格的ActualWidth属性来实现这一点,您甚至可以更进一步,例如,如果您想通过使用转换器将高度设置为等于宽度的两倍:

代码示例1:

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

代码示例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>
相关问题