【问题标题】:Grid row height ratio based on content基于内容的网格行高比
【发布时间】:2014-12-06 07:27:32
【问题描述】:

我有一个两行的网格。在每一行中都有一个 ListView。 第一行有 100 个项目,第二行有 500 个项目。 这不适合屏幕,所以我想在每个 ListView 上显示滚动条(没问题),并且行高应该与 ListViews 的高度成比例。像 1* 和 5* 但我事先不知道这些 ListBox 会有多大。

注意:每个项目可能有不同的大小,所以我想使用实际的 ListBox 高度而不是项目数

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition /> <!-- keep ratio of rows based on Listbox Heights -->
        <RowDefinition />
    </Grid.RowDefinitions>
    <ListBox Grid.Row="0">
        <!-- 100 items -->
    </ListBox>
    <ListBox Grid.Row="1">
        <!-- 500 items -->
    </ListBox>
</Grid>

有什么想法吗?

【问题讨论】:

    标签: wpf


    【解决方案1】:
     <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="{Binding Height,ElementName=ListBox1}"/>
            <!-- keep ratio of rows based on Listbox Heights -->
            <RowDefinition Height="{Binding Height,ElementName=ListBox2}"/>
        </Grid.RowDefinitions>
        <ListBox Grid.Row="0" x:Name="ListBox1">
            <!-- 100 items -->
        </ListBox>
        <ListBox Grid.Row="1" x:Name="ListBox2">
            <!-- 500 items -->
        </ListBox>
    </Grid>
    

    如果你有更复杂的场景,你可以使用转换器。

    【讨论】:

    • 这行不通。这将设置绝对高度而不是 * 比率。我使用了“StarConverter”,但它也不起作用,因为 Lisbox 的 Height 或 ActualHeight 是屏幕上显示的,而不是 Listbox 中所有元素的高度 :(
    猜你喜欢
    • 2022-12-06
    • 2018-09-19
    • 2021-04-05
    • 2010-11-30
    • 2012-05-04
    • 2022-01-21
    • 2014-02-07
    • 2021-04-12
    • 2023-03-16
    相关资源
    最近更新 更多