前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >wpf wrapPanel居中并从左到右排列

wpf wrapPanel居中并从左到右排列

作者头像
全栈程序员站长
发布2022-09-15 10:06:29
3460
发布2022-09-15 10:06:29
举报
文章被收录于专栏:全栈程序员必看

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

代码语言:javascript
复制
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

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档