Autostretch xaml按宽度控制,并根据其宽度设置高度?

Autostretch xaml按宽度控制,并根据其宽度设置高度?,xaml,layout,height,width,stretch,Xaml,Layout,Height,Width,Stretch,我想自动拉伸控件以按宽度调整网格,然后将其高度设置为网格宽度 有什么办法可以做到这一点吗?您可以通过将“高度”属性绑定到栅格的“实际宽度”属性来实现这一点,甚至可以更进一步,例如,如果您希望使用转换器将高度设置为宽度的两倍: 代码示例1: <Grid x:Name="ContentPanel" Grid.Row="1"> <Button x:Name="Button" Content="Button" Width="200" Height="{Binding E

我想自动拉伸控件以按宽度调整网格,然后将其高度设置为网格宽度


有什么办法可以做到这一点吗?

您可以通过将“高度”属性绑定到栅格的“实际宽度”属性来实现这一点,甚至可以更进一步,例如,如果您希望使用转换器将高度设置为宽度的两倍:

代码示例1:

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

代码示例2:

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

您的意思是“网格高度是多少”??