防止 WPF ListView 或 ListBox 显示“一半”项目

2024-04-16

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

这是我们不希望出现的行为。

从我到目前为止的研究来看,似乎没有办法阻止 ListBox 或 ListView 显示部分项目,但也许有人找到了另一种方法来处理这个问题。也许该物品在只有一半可见时会触发自身隐形。但我们怎样才能知道呢?

我们愿意接受任何建议。


我不知道如何以这种方式修复 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;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

防止 WPF ListView 或 ListBox 显示“一半”项目 的相关文章

随机推荐