【问题标题】:How do I set the ListView height to the height of the contining row in XAML如何将 ListView 高度设置为 XAML 中连续行的高度
【发布时间】:2017-11-27 05:16:31
【问题描述】:

我无法弄清楚告诉ListView它的高度是包含ListView的行的高度的语法,即ListView的Parent。 这是用户控件中的 xaml。我已经放了我认为可行的东西,但没有。我把它放在那里,所以你会看到我在尝试什么。

<ListView Grid.Row="1" 
Height="{Binding Path=Height,RelativeSource={RelativeSource AncestorType=Grid.Row}}"
ItemsSource="{Binding Folders}"
ItemTemplate="{StaticResource FolderListTemplate}"
SelectionMode="Single"
SelectedIndex="1"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Margin="3"
ScrollViewer.VerticalScrollBarVisibility="Visible"
ScrollViewer.CanContentScroll="True"
/>

谢谢,

【问题讨论】:

  • 除了 AncestorType=Grid.Row 没有意义(因为 Grid.Row 不是类型),只需删除高度分配。默认情况下,Grid 将自动调整其子元素的大小。阅读有关 Grid 类的在线文档,尤其是它的 RowDefinitions 和 ColumnDefinitions 属性。
  • 感谢您的回复,威尔。您的图像显示了我在添加 height 属性之前所拥有的内容。行定义包括设置为 Auto 的第 0 行和设置为 * 的第 1 行。我命名了网格,然后将 Height 属性设置为元素的 ActualHeight。问题在于 listView 低于可见区域。

标签: wpf xaml listview binding


【解决方案1】:

只需将 RowDefinition 高度设置为 Auto,这是我的 xaml:

<Grid Background="DimGray">
        <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>
        <StackPanel Background="CornflowerBlue"/>
        <ListView Grid.Row="1" 
                  x:Name="ListView"
                  ItemsSource="{Binding Items}"
                  SelectionMode="Single"
                  SelectedIndex="1"
                  VerticalAlignment="Stretch"
                  HorizontalAlignment="Stretch"
                  Margin="3"
                  ScrollViewer.VerticalScrollBarVisibility="Visible"
                  ScrollViewer.CanContentScroll="True"/>

        <StackPanel Grid.Row="2" Background="CornflowerBlue"/>
    </Grid>

然后输出:

如果您使用星号表示法,那么该行将采用您给出的星号值的可用高度,在这种情况下 1* 与 ***** 相同。 如果我修改为像你一样使用星值:

<Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="50"/>
        </Grid.RowDefinitions>

输出:

希望对你有帮助

【讨论】:

  • 感谢您的回复。我没有在原始问题中输入足够的代码。如果我有,每个人都会看到问题不在于我原来的 ListView(没有设置高度属性),而是在于 HeaderedContentControl,它是 Row 中的第一项。我只是把我的行设置错了,这造成了问题。当我的清单不合适时,我一直在看它而不是更大的图景。当我删除 Headered 内容控件以减少混乱时,我的列表视图会自行修复。我将此标记为答案,因为它对将来的某人会更有用。
猜你喜欢
  • 2015-03-16
  • 2015-09-24
  • 2016-02-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
相关资源
最近更新 更多