【问题标题】:Prevent WPF ListView or ListBox from showing "half" Items防止 WPF ListView 或 ListBox 显示“一半”项
【发布时间】:2011-05-18 13:15:42
【问题描述】:

在我们的应用程序中,我们在网格中有一些 ListViews 和 ListBoxes,您可以在网格拆分器的帮助下更改控件的实际高度。 这样做时,您可以安排 ListBox 的高度,以便其中一项不完全可见,因为 ListView 变得太短而无法显示。

这是我们不希望的行为。

从我目前的研究来看,似乎没有办法阻止 ListBox 或 ListView 显示部分项目,但也许有人找到了另一种方法来处理这个问题。当它只有一半可见时,也许该项目可以触发自身不可见。但是我们怎么知道呢?

我们愿意接受任何建议。

【问题讨论】:

  • 为什么要阻止部分项目显示?只要列表是可滚动的,这就是向人们表明内容可以滚动的一个很好的指标。

标签: wpf listview listbox


【解决方案1】:

我不知道以这种方式修复 ListView。但是,对于 ListBox,您可以覆盖 ArrangeOverride 并自己排列项目。把你想看的东西堆起来,把你不希望看到的东西(例如,部分可见的东西)排列起来,让它们不可见。例如非虚拟化版本:

    /// <summary>
    /// Used to arrange all of the child items for the layout.
    /// Determines position for each child.
    /// </summary>
    /// <param name="finalSize">Parent passes this size in</param>
    /// <returns>Parent size (always uses all of the allowed area)</returns>
    protected override Size ArrangeOverride(Size finalSize)
    {
        // Arrange in a stack
        double curX = 0;
        double curY = 0;
        foreach (UIElement child in InternalChildren)
        {
            double nextY = curY + child.DesiredSize.Height;
            if (nextY > finalSize.Height)         // Don't display partial items
                child.Arrange(new Rect());
            else
                child.Arrange(new Rect(curX, curY, child.DesiredSize.Width, child.DesiredSize.Height);
            curY = nextY;
        }

        return finalSize;
    }

【讨论】:

    【解决方案2】:

    您只需将 MinHeight 或 MinWidth 设置为要调整大小的单元格。试试这个:

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition MinHeight="50"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition MinHeight="50"/>
            </Grid.RowDefinitions>
            <ListBox HorizontalAlignment="Center" Margin="10">
                <ListBoxItem>First</ListBoxItem>
                <ListBoxItem>Second</ListBoxItem>
                <ListBoxItem>Third</ListBoxItem>
                <ListBoxItem>Fourth</ListBoxItem>
            </ListBox>
            <GridSplitter Grid.Row="1" Height="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
            <ListView Grid.Row="2" Margin="4">
                <ListViewItem>First</ListViewItem>
                <ListViewItem>Second</ListViewItem>
                <ListViewItem>Third</ListViewItem>
                <ListViewItem>Fourth</ListViewItem>
            </ListView>
        </Grid>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-12
      • 2023-03-27
      • 1970-01-01
      相关资源
      最近更新 更多