业务有这样的需求,类似瀑布流。内容两列不等高展示。

只需要继承panel,重写MeasureOverride和ArrangeOverride方法就行了。

很简单,内容都在代码里。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls; namespace XXXXXX
{
public class TwoColumnPanel : Panel
{
/// <summary>
/// 先测量需要多大尺寸,做个申报准备
/// </summary>
/// <param name="constraint">限定的尺寸,比如,规定了width和height</param>
/// <returns></returns>
protected override Size MeasureOverride(Size constraint)
{
//定义预期的宽度和高度
double height = 0, height1 = 0, height2 = 0, width = 0;
UIElement element;
if (Children.Count > 0)
{
element = Children[0];
width = element.DesiredSize.Width*2;
}
//遍历每个元素,计算所需的总尺寸
for (int i = 0; i < Children.Count; i++)
{
element = Children[i];
//按照限定的尺寸测量一下自己,拿镜子找着自己
element.Measure(constraint);
if (i % 2 == 0)
{
height1 += element.DesiredSize.Height;
}
else
{
height2 += element.DesiredSize.Height;
}
}
height = height1 > height2 ? height1 : height2; //申报,我需要这个尺寸
return new Size(width, height);
} /// <summary>
/// 排列每个元素
/// </summary>
/// <param name="arrangeBounds">测量的尺寸</param>
/// <returns></returns>
protected override Size ArrangeOverride(Size arrangeBounds)
{
double currentX2 = 0,currentY1 = 0,currentY2 = 0;
UIElement element; if (Children.Count > 0)
{
element = Children[0];
currentX2 = element.DesiredSize.Width;
} for (int i = 0; i < Children.Count; i++)
{
element = Children[i];
//排列每个元素
if (i % 2 == 0)
{
Children[i].Arrange(new Rect(0, currentY1, element.DesiredSize.Width, element.DesiredSize.Height));
currentY1 += element.DesiredSize.Height;
}
else
{
Children[i].Arrange(new Rect(currentX2, currentY2, element.DesiredSize.Width, element.DesiredSize.Height));
currentY2 += element.DesiredSize.Height;
} }
return arrangeBounds;
}
}
}

  

wpf ListBox,item两列不等高。的更多相关文章

  1. 多种方法实现div两列等高(收集整理)

    HTML骨架 <div id="header">头部</div> <div id ="container"> <div ...

  2. JQuery 实现两列等高并自适应高度

    想要使用 JQuery 实现两列等高并自适应高度,其实也很简单,原理就是取得左右两边的高度,然后判断这个值,把大的值赋给小的就行了.看代码: $(document).ready(function() ...

  3. 两列等高布局 padding+margin的负值 CSS布局奇淫技巧之-多列等高

    代码: 效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...

  4. 纯css实现两列等高

    <!doctype html> <html> <head> <meta /> <title>Title</title> < ...

  5. css两列等高布局

    布局方案 等高布局有几种不同的方法,但目前为止我认为浏览器兼容最好最简便的应该是padding补偿法.首先把列的padding-bottom设为一个足够大的值,再把列的margin-bottom设一个 ...

  6. 利用jQuery进行三行两列等高布局

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. 布局两列div等高方法

    一.左右布局,左侧div绝对定位,外div相对定位 <!DOCTYPE html> <html lang="en"> <head> <me ...

  8. 两列布局,读《css那些事儿》

    两列布局: 1.两列定宽: 要点:float.width固定. :after清除浮动. 前提:两列的盒模型宽度相加不能大于父元素的宽度,否则会出现错位现象. <!DOCTYPE html> ...

  9. flex 垂直居中、两列对齐、自适应宽

    flex 垂直居中 <div id="parent"> <div id="child"> </div> </div&g ...

随机推荐

  1. Servlet题库

    一.    填空题 Servlet中使用Session对象的步骤为:调用  HttpServletRequest.getSession()  得到Session对象,查看Session对象,在会话中保 ...

  2. September 24th 2016 Week 39th Saturday

    The worst solitude is to be destitute of sincere friendship. 最大的孤独莫过于没有真诚的友谊. I walk slowly, but I n ...

  3. sql 查询最近30分钟或者自定义时间数据

    ) FROM dual; 30分钟或者自定义

  4. 数独挑战(codevs 2924)

    2924 数独挑战  时间限制: 1 s  空间限制: 1000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description “芬兰数学家因卡拉,花费3 ...

  5. 利用CocoaPods,在项目中导入AFNetworking类库

    场景1:利用CocoaPods,在项目中导入AFNetworking类库 AFNetworking类库在GitHub地址是:https://github.com/AFNetworking/AFNetw ...

  6. 数据结构之Dijkstra算法

    基本思想 通过Dijkstra计算图G中的最短路径时,需要指定起点s(即从顶点s开始计算). 此外,引进两个集合S和U.S的作用是记录已求出最短路径的顶点(以及相应的最短路径长度),而U则是记录还未求 ...

  7. 二、JavaScript语言--JS实践--信息滚动效果制作

    运用JavaScript技术,掌握无缝滚动和歇间性滚动的制作方法. 一.marquee标签实现信息滚动 1 behavior滚动的方式 alternate:表示在两端之间来回滚动 scroll:表示由 ...

  8. JS 获取浏览器窗口大小

    JS 获取浏览器窗口大小 <script> // 获取窗口宽度 if (windows.innerWidth) { winWidth = windows.innerWidth; } els ...

  9. Jquery学习笔记--性能优化建议

    一.选择器性能优化建议 1. 总是从#id选择器来继承 这是jQuery选择器的一条黄金法则.jQuery选择一个元素最快的方法就是用ID来选择了. 1 $('#content').hide(); 或 ...

  10. MongoDB增删查改

    1.insert db.Customers.insert({ "DateTest":new Date(), "IntTest":32, "Double ...