WPF / Metro风格:制作的ListView只显示完整的项目 [英] WPF/Metro-style: Making ListView show only complete items

查看:279
本文介绍了WPF / Metro风格:制作的ListView只显示完整的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Metro应用,我有一个包含一定数量的项目的数据源(例如25)。我有presents这些项目一个ListView。我的问题是,在ListView的尺寸允许其以显示,就是说,6.5项,以便它显示的最后一个项目中的一半被切断。如果分辨率变化,它可能会显示4项,或8.2的项目,或什么的。我想的是,ListView控件显示正是适合在控制的高度,而不是剪辑的最后一个项目的项目数。

In my Metro application, I have a data source containing a certain number of items (say 25). I have a ListView that presents those items. My problem is that the ListView have a size that allows it to display, say, 6.5 items, so that the last item it displays is cut in half. If the resolution changes, it might display 4 items, or 8.2 items, or whatever. What I'd like is that the ListView shows exactly the number of items that fits in the height of the control, instead of clipping the last item.

现在,我看到了两个可能的半码的解决方案,其中没有一个是最佳的:

Right now, I see two possible half-solutions, none of which is optimal:


  1. 在ListView的高度设置为一个固定的高度,它是项目大小的倍数。这不,在更改分辨率缩放。

  1. Set the height of the ListView to a fixed height that is a multiple of the item size. This does not scale with changes in resolution.

限制在数据源中的项目数。这并不规模无论是。

Limit the number of items in the data source. This does not scale either.

所以我的问题是,怎样才能让我在ListView只显示完整的项目(项目,所有边缘视/列表视图中),并隐藏其他人呢?

So my question is, how can I get the ListView to only display complete items (items where all edges are inside the viewport/listview), and hide the rest?

在此先感谢!

推荐答案

我的最后的解决办法是@NovitchiS和@JesuX的建议结合起来。

My final solution was to combine the suggestions of @NovitchiS and @JesuX.

我创建了一个堆叠面板覆盖,并听取了LayoutUpdated事件。我的最终解决方案:

I created a stack panel override, and listened to the LayoutUpdated event. My final solution:

class HeightLimitedStackPanel : StackPanel
{
    public HeightLimitedStackPanel() : base()
    {
        this.LayoutUpdated += OnLayoutUpdated;
    }

    double GetSizeOfVisibleChildren(double parentHeight)
    {
        double currentSize = 0;
        bool hasBreaked = false;
        for (int i = 0; i < Children.Count; i++)
        {
            var child = Children[i];
            if (currentSize + child.DesiredSize.Height > parentHeight)
            {
                hasBreaked = true;
                break;
            }
            currentSize += child.DesiredSize.Height;
        }
        if (hasBreaked) return currentSize;

        return parentHeight;
    }

    double ParentHeight
    {
        get 
        {
            ItemsPresenter parent = VisualTreeHelper.GetParent(this) as ItemsPresenter;
            if (parent == null)
                return 0;

            return parent.ActualHeight;
        }
    }

    double previousHeight = 0;
    int previousChildCount = 0;
    protected void OnLayoutUpdated(object sender, object e)
    {
        double height = ParentHeight;
        if (height == previousHeight && previousChildCount == Children.Count) return;
        previousHeight = height;
        previousChildCount = Children.Count;

        this.Height = GetSizeOfVisibleChildren(height);
    }
}

这篇关于WPF / Metro风格:制作的ListView只显示完整的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆