wpf wrapPanel居中并从左到右排列

wpf wrapPanel居中并从左到右排列publicclassAlignableWrapPanel:Panel{///<summary>///注册新的属性HorizontalContentAlignment///</summary>publicHorizontalAlignmentHorizontalContentAlignment{get{return(Horizont.

大家好,又见面了,我是你们的朋友全栈君。

public class AlignableWrapPanel : Panel
    {
        /// <summary>
        /// 注册新的属性 HorizontalContentAlignment
        /// </summary>
        public HorizontalAlignment HorizontalContentAlignment
        {
            get { return (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty); }
            set { SetValue(HorizontalContentAlignmentProperty, value); }
        }

        public static readonly DependencyProperty HorizontalContentAlignmentProperty =
            DependencyProperty.Register("HorizontalContentAlignment", 
                typeof(HorizontalAlignment), 
                typeof(AlignableWrapPanel), 
                new FrameworkPropertyMetadata(HorizontalAlignment.Left, FrameworkPropertyMetadataOptions.AffectsArrange));
        /// <summary>
        /// panel尺寸的计算
        /// </summary>
        /// <param name="constraint"></param>
        /// <returns></returns>
        protected override Size MeasureOverride(Size constraint)
        {
            //当前行的尺寸
            Size curLineSize = new Size();

            //整个panel的尺寸
            Size panelSize = new Size();

            //panel里的item
            UIElementCollection children = base.InternalChildren;

            for (int i = 0; i < children.Count; i++)
            {
                UIElement child = children[i] as UIElement;

                // 更新子元素在容器中的尺寸
                child.Measure(constraint);
                Size sz = child.DesiredSize;

                //当需要换行时
                if (curLineSize.Width + sz.Width > constraint.Width) 
                {
                    panelSize.Width = Math.Max(curLineSize.Width, panelSize.Width);
                    panelSize.Height += curLineSize.Height;
                    curLineSize = sz;

                    //当子元素宽度大于容器时,给予新的一行
                    if (sz.Width > constraint.Width)                 
                    {
                        panelSize.Width = Math.Max(sz.Width, panelSize.Width);
                        panelSize.Height += sz.Height;
                        curLineSize = new Size();
                    }
                }
                else //不需要换行时,也继续加一行
                {
                    curLineSize.Width += sz.Width;
                    curLineSize.Height = Math.Max(sz.Height, curLineSize.Height);
                }
            }

            // 最后一行的尺寸
            panelSize.Width = Math.Max(curLineSize.Width, panelSize.Width);
            panelSize.Height += curLineSize.Height;

            return panelSize;
        }

        /// <summary>
        /// 换行的方法
        /// </summary>
        /// <param name="arrangeBounds"></param>
        /// <returns></returns>
        protected override Size ArrangeOverride(Size arrangeBounds)
        {
            int firstInLine = 0;
            Size curLineSize = new Size();
            double accumulatedHeight = 0;
            UIElementCollection children = this.InternalChildren;

            for (int i = 0; i < children.Count; i++)
            {
                Size sz = children[i].DesiredSize;

                if (curLineSize.Width + sz.Width > arrangeBounds.Width) 
                {
                    //安排元素在行中的位置
                    ArrangeLine(accumulatedHeight, curLineSize, arrangeBounds.Width, firstInLine, i);

                    
                    accumulatedHeight += curLineSize.Height;
                    curLineSize = sz;

                    if (sz.Width > arrangeBounds.Width)                   
                    {
                        //安排元素在行中的位置
                        ArrangeLine(accumulatedHeight, sz, arrangeBounds.Width, i, ++i);
                        
                        accumulatedHeight += sz.Height;
                        curLineSize = new Size();
                    }
                    firstInLine = i;
                }
                else 
                {
                    curLineSize.Width += sz.Width;
                    curLineSize.Height = Math.Max(sz.Height, curLineSize.Height);
                }
            }

            if (firstInLine < children.Count)
            {
                //安排元素在行中的位置
                ArrangeLine(accumulatedHeight, curLineSize, arrangeBounds.Width, firstInLine, children.Count);
            }

            return arrangeBounds;
        }
        /// <summary>
        /// 行中item的排列方式
        /// </summary>
        /// <param name="y"></param>
        /// <param name="lineSize"></param>
        /// <param name="boundsWidth"></param>
        /// <param name="start"></param>
        /// <param name="end"></param>
        private void ArrangeLine(double y, Size lineSize, double boundsWidth, int start, int end)
        {
            var children = InternalChildren;

            var x = 0D;
            if (HorizontalContentAlignment == HorizontalAlignment.Center)
            {
                //item起点的位置
                x = 0D;
            }

            //安排每个item在一行中的位置
            for (var i = start; i < end; i++)
            {
                var child = children[i];
                double itemWidth;
                itemWidth = child.DesiredSize.Width;
                

                child.Arrange(new Rect(x, y, itemWidth, lineSize.Height));
                //x每多一个item就加上自己本身的距离,作为起点
                x += itemWidth;
            }
        }
       
    }

参考文章.net – WPF – 如何将WrapPanel中的所有项目居中? – Thinbug

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163338.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • idea最新激活码 3月最新注册码

    idea最新激活码 3月最新注册码,https://javaforall.cn/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

  • 股票打板策略分析_打板选股技巧

    股票打板策略分析_打板选股技巧股票打板策略分析这里我们只分析一件事情,就是如何打板才能最大概率赚到钱,就是我们可以分析过去一天涨停今天还涨停、分析过去两天涨停今天涨的概率,一直到过去10天涨停今天涨的概率,其实很多人都喜欢打板,但是可能大家都没分析过打板的胜率。前面我们已经可以筛选出截止到特定日期的过去10天中的连续涨停了,这里我们只需要将所有日期过去10日的连续涨停计算出来就可以作为我们的数据源,然后计算统计个数算分布就可以了,至于如何计算连续涨停可以参考股票数据分析计算历史数据的涨停情况我们今天的打板分析,是在昨天的基础上,

    2022年10月21日
  • Pytorch-BN层详细解读

    Pytorch-BN层详细解读Pytorch-BN层BN解决了InternalCovariateShift问题机器学习领域有个很重要的假设:独立同分布假设,即假设训练数据和测试数据是满足相同分布的。我们知道:神经网络的训练实际上就是在拟合训练数据的分布。如果不满足独立同分布假设,那么训练得到的模型的泛化能力肯定不好。再来思考一个问题:为什么传统的神经网络要求将数据归一化(训练阶段将训练数据归一化并记录均值和方差,测试…

    2022年10月14日
  • 软件缺陷,缺陷报告怎么写_缺陷报告通常包括哪些内容

    软件缺陷,缺陷报告怎么写_缺陷报告通常包括哪些内容软件缺陷软件缺陷:常常又被叫做Bug。所谓软件缺陷,即为计算机软件或程序中存在的某种破坏正常运行能力的问题、错误,或者隐藏的功能缺陷。缺陷的存在会导致软件产品在某种程度上不能满足用户的需要。从软件测试观点出发,软件缺陷有以下五大类:功能缺陷、系统缺陷、加工缺陷、数据缺陷、代码缺陷软件类别:缺陷的表现形式不仅体现在功能的失效方面,还体现在其他方面。主要类型有:软件没有实现产品规格说明所…

  • java final keyword

    java final keyword

  • Android语音采集两种方式MediaRecorder和AudioRecord

    Android语音采集两种方式MediaRecorder和AudioRecord

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号