【问题标题】:Autostretch xaml control width wise and set height based on its width?自动拉伸 xaml 控件宽度并根据其宽度设置高度?
【发布时间】:2014-03-06 16:33:35
【问题描述】:

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

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

【问题讨论】:

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

标签: xaml layout height width stretch


【解决方案1】:

您可以通过将 Height 属性绑定到 Grid 的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-27
    • 1970-01-01
    • 2014-01-22
    • 1970-01-01
    • 1970-01-01
    • 2013-02-07
    • 1970-01-01
    • 2018-05-09
    相关资源
    最近更新 更多