【问题标题】:WPF Stretch ListBox Height 100% of Grid.Row?WPF Stretch ListBox Height 100% of Grid.Row?
【发布时间】:2011-07-19 00:15:52
【问题描述】:

我正在尝试将ListBox 的高度拉伸为父网格高度的 100%(即父视图高度的 90%);即使listboxes 为空。我应该注意到VerticalAlignment="Stretch" 似乎不起作用,所以我已经从ListBox 和StackPanel 元素中删除了它。截至目前,ListBox 只延伸到它需要的范围,以容纳它包含的项目数量。我知道行定义应该可以工作,但如果两个列表都是空的,它们都会缩小到几个像素高(连同网格行)。尽管有明确的高度声明,有什么东西会导致这些行缩小吗?

<Grid.ColumnDefinitions>
        <ColumnDefinition Width=".24*"/>
        <ColumnDefinition Width=".73*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height=".9*"/>
        <RowDefinition Height=".1*"/>
    </Grid.RowDefinitions>
    <ListBox Grid.Column="0" Grid.Row="0" Name="Subdivisions" SelectedItem="{Binding SelectedSubdivisionViewModel}" ItemsSource="{Binding Path=Subdivisions}" Grid.IsSharedSizeScope="True">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel>
                    <Border BorderBrush="#FF4788c8" BorderThickness="1,1,1,1" CornerRadius="8,8,8,8">
                        <Expander IsExpanded="{Binding IsSelected, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListBoxItem}}}">
                            <Expander.Header>
                                <StackPanel>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*" SharedSizeGroup="col1" />
                                            <ColumnDefinition Width=".1*" SharedSizeGroup="col2" />
                                            <ColumnDefinition Width="*" SharedSizeGroup="col3" />
                                        </Grid.ColumnDefinitions>
                                        <Grid.RowDefinitions>
                                            <RowDefinition/>
                                            <RowDefinition/>
                                        </Grid.RowDefinitions>
                                        <TextBlock Grid.Column="0" Grid.Row="0">
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="Name: {0}">
                                                  <Binding Path="SubdivisionName" />
                                                  <Binding Path="SubdivisionID" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                        <TextBlock Grid.Column="2" Grid.Row="0">
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="ID: {0}">
                                                  <Binding Path="SubdivisionName" />
                                                  <Binding Path="SubdivisionID" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                        <TextBlock Grid.Column="2" Grid.Row="1">
                                            <TextBlock.Text>
                                                <MultiBinding StringFormat="ID: {0}">
                                                  <Binding Path="SubdivisionName" />
                                                  <Binding Path="SubdivisionID" />
                                                </MultiBinding>
                                            </TextBlock.Text>
                                        </TextBlock>
                                    </Grid>
                                </StackPanel>
                            </Expander.Header>
                            <StackPanel>
                                <Grid>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition/>
                                        <ColumnDefinition/>
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <TextBlock Text="{Binding ElementName=SubdivisionID}" />
                                    <TextBlock Text="{Binding Path=SubdivisionID}" />
                                </Grid>
                            </StackPanel>
                        </Expander>
                    </Border>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

【问题讨论】:

  • 你看到了什么?当我显示这个时,ListBox 是窗口高度的 90%,这是我希望在 的行中看到的。
  • 第一行似乎不符合 90% 的高度定义。
  • 在我的例子中,GroupBox 在App.xaml 引用的资源字典中有一个样式。 VerticalAlignment 设置为 Top。我在本地将属性设置为Stretch。这不能回答问题,但可以帮助有类似问题的人。

标签: wpf xaml listbox height expression-blend


【解决方案1】:

您发布的代码完全符合父网格的行高定义所声明的内容:占据可用高度的 90%。

*.1 = 高度的 10%
*.9 = 高度的 90%

通常,从 xaml 中移除杂乱无章并从有助于布局的简单内容开始很有用。这是一个示例,其中包含代码的 Grid 列/行定义,但杂乱程度较低,并且有一些背景颜色可以显示整个 ListBox。

  • 第一个 ListBox 有几个项目,而第二个 ListBox 只有几个项目。
  • 两个 ListBox 都位于第一行并占据了 90% 的可用空间。
  • 第二行包含一个填充其余空间的网格;你可以看到它占用了 10% 的可用空间。

请注意,第一个 ListBox 没有声明 Column 或 Row 索引;不使用索引时,假定为0,即Grid.Row="0" Grid.Column=0。

<Grid Background="Red">

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width=".24*"/>
        <ColumnDefinition Width=".73*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height=".9*"/>
        <RowDefinition Height=".1*"/>
    </Grid.RowDefinitions>

    <ListBox Background="LightGray"
             ItemsSource="{x:Static Fonts.SystemFontFamilies}"/>

    <ListBox Grid.Column="1" Grid.Row="0" Background="LightSlateGray">
        <ListBoxItem>John</ListBoxItem>
        <ListBoxItem>Jane</ListBoxItem>
        <ListBoxItem>Fido</ListBoxItem>
    </ListBox>

    <Grid Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Background="Tomato" />

</Grid>

【讨论】:

  • 我很欣赏这次尝试,但它对您来说似乎是正确的原因是因为您已经在左侧列表中填充了足够多的项目以最大化声明的 90% 网格行高。我需要它通过“即使列表为空”来拉伸网格的整个高度。
  • @alan - 一点也不。取出第一个 ListBox,第二个 ListBox 仍将填满整个垂直空间。不知道为什么这被否决了。
  • @alan - 以我为例,运行它。两个 ListBox 都展开以填充整个高度。然后从 XAML 中取出第一个 ListBox(绑定到字体的那个),运行示例,第二个 ListBox 仍然填充整个高度。
【解决方案2】:

我能够通过下面的 XAML 将 ListBox 高度属性绑定到 LayoutRoot Grid 的 ActualHeight 来实现所需的高度:

<Grid x:Name="LayoutRoot" Background="LightGray">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width=".24*"/>
        <ColumnDefinition Width=".73*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height=".9*"/>
        <RowDefinition Height=".1*"/>
    </Grid.RowDefinitions>
    <ListBox Name="Subdivisions" SelectedItem="{Binding SelectedSubdivisionViewModel}" ItemsSource="{Binding Path=Subdivisions}" Grid.IsSharedSizeScope="True" Height="{Binding ElementName=LayoutRoot, Path=ActualHeight}" >

重要的是:

Height="{Binding ElementName=LayoutRoot, Path=ActualHeight}"

也可以通过祖先类型实现:

Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid}}}"

【讨论】:

  • 如果您希望ListBox 的高度与行相同,请为行命名。然后,在Binding 中,将ElementName 更改为您的姓名。
【解决方案3】:

堆栈面板使其压缩。删除它,它会填满整个高度

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2017-01-24
    • 1970-01-01
    • 2010-09-16
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    相关资源
    最近更新 更多