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

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • [Linux]F5负载均衡器「建议收藏」

    [Linux]F5负载均衡器「建议收藏」F5负载均衡器是硬件的负载均衡设备F5配置最简单负载均衡,需要配置的参数有Node(节点)、Pool(资源池)、和VirtualServer(虚拟服务器),它们的关系是,先配置Node,然后配置V

    2022年8月2日
    5
  • Kettle详细使用教程

    Kettle详细使用教程Kettle详细使用教程文章内容链接Kettle插入更新、自定义常量数据与删除、增加常量、增加序列Kettle插入更新、自定义常量数据与删除、增加常量、增加序列Kettle字段选择、剪切字符串、字符串替换、Concatfields、字符串操作Kettle字段选择、剪切字符串、字符串替换、Concatfields、字符串操作Kettle排序记录、去除重复记录、拆分字段、值映射、替换NULL值Kettle排序记录、去除重复记录、拆分字段、值映射、替换NULL值

    2022年5月10日
    57
  • HTTP默认端口_http协议使用的端口号

    HTTP默认端口_http协议使用的端口号HTTP默认端口80是http协议的默认端口,是在输入网站的时候其实浏览器(非IE)已经帮你输入协议了,所以你输入http://baidu.com,其实是访问http://baidu.com:80。而8080,一般用与webcahe,完全不一样的两个,比如linux服务器里apache默认跑80端口,而apache-tomcat默认跑8080端口,其实端口没有实际意义只是一个接口,主要是看服务的监听端口。443是https的默认端口端口号标识了一个主机上进行通信的不同的应用程序。 H.

    2025年12月5日
    2
  • android acitivity 跳转到fragment,android Activity跳转到指定的Fragment

    android acitivity 跳转到fragment,android Activity跳转到指定的Fragment在要跳转的activity中的按钮写://一、先跳转到主MyActivityFragment,通过传递参数让他接受caseR.id.grxxbut:Intentshow=newIntent(GrXxActivity.this,MyActivityFragment.class);show.putExtra(“grxx”,1);startActivity(show);finish();break…

    2022年5月21日
    55
  • STM32的指令周期[通俗易懂]

    STM32的指令周期[通俗易懂]在keil中编程时,写了一行代码,然后就想知道,执行这句C代码需要多长时间。时钟周期在这就不解释了,频率的倒数。指令周期,个人理解就是cpu执行一条汇编指令所需要的时间。我们知道cm3使用的三级流水线,那么到底一条指令的执行需要多少个时钟周期。下面通过keil软件仿真,来计算一个指令所需的时钟周期。 使用STM32F103RC,。配置其主时钟HCLK为72mhz测试代码如下:…

    2022年10月13日
    6
  • VMwware15激活码【中文破解版】2022.02.10

    (VMwware15激活码)最近有小伙伴私信我,问我这边有没有免费的intellijIdea的激活码,然后我将全栈君台教程分享给他了。激活成功之后他一直表示感谢,哈哈~https://javaforall.net/100143.htmlIntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,上面是详细链接哦~4KDD…

    2022年4月1日
    58

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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