当前位置:网站首页>wpf wrapPanel居中并从左到右排列
wpf wrapPanel居中并从左到右排列
2022-07-31 05:09:00 【JOJOL1N】
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;
}
}
}边栏推荐
猜你喜欢

Quickly master concurrent programming --- the basics

Distributed Transactions - Introduction to Distributed Transactions, Distributed Transaction Framework Seata (AT Mode, Tcc Mode, Tcc Vs AT), Distributed Transactions - MQ

MySQL-Explain详解

pycharm专业版使用

Sword Point Offer Special Assault Edition ---- Day 2
![2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。](/img/7f/130a9b733855a2bab07d26ffda2c49.png)
2022-07-30:以下go语言代码输出什么?A:[]byte{} []byte;B:[]byte{} []uint8;C:[]uint8{} []byte;D:[]uin8{} []uint8。

面试官问我TCP三次握手和四次挥手,我真的是

C语言实验一 熟悉C程序的环境

C语言实验五 循环结构程序设计(二)

Flask-based three-party login process
随机推荐
torch.normal function usage
pytorch中的一维、二维、三维卷积操作
运用flask框架发送短信验证码的流程及具体代码
Anaconda配置环境指令
关于小白安装nodejs遇到的问题(npm WARN config global `--global`, `--local` are deprecated. Use `--location=glob)
Three handshakes and four waves
基于flask的三方登陆的流程
数据库上机实验7 数据库设计
The interviewer asked me TCP three handshake and four wave, I really
Pytorch教程Introduction中的神经网络实现示例
110 MySQL interview questions and answers (continuously updated)
Kubernetes 证书可用年限修改
torch.normal函数用法
剑指offer基础版 ----- 第25天
C语言文件读、写、定位函数
C语言如何分辨大小端
剑指offer基础版 --- 第24天
Kubernetes加入集群的TOKEN值过期
【MQ我可以讲一个小时】
Minio upload file ssl certificate is not trusted