wpf ListBox,item两列不等高。
业务有这样的需求,类似瀑布流。内容两列不等高展示。
只需要继承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两列不等高。的更多相关文章
- 多种方法实现div两列等高(收集整理)
HTML骨架 <div id="header">头部</div> <div id ="container"> <div ...
- JQuery 实现两列等高并自适应高度
想要使用 JQuery 实现两列等高并自适应高度,其实也很简单,原理就是取得左右两边的高度,然后判断这个值,把大的值赋给小的就行了.看代码: $(document).ready(function() ...
- 两列等高布局 padding+margin的负值 CSS布局奇淫技巧之-多列等高
代码: 效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/ ...
- 纯css实现两列等高
<!doctype html> <html> <head> <meta /> <title>Title</title> < ...
- css两列等高布局
布局方案 等高布局有几种不同的方法,但目前为止我认为浏览器兼容最好最简便的应该是padding补偿法.首先把列的padding-bottom设为一个足够大的值,再把列的margin-bottom设一个 ...
- 利用jQuery进行三行两列等高布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 布局两列div等高方法
一.左右布局,左侧div绝对定位,外div相对定位 <!DOCTYPE html> <html lang="en"> <head> <me ...
- 两列布局,读《css那些事儿》
两列布局: 1.两列定宽: 要点:float.width固定. :after清除浮动. 前提:两列的盒模型宽度相加不能大于父元素的宽度,否则会出现错位现象. <!DOCTYPE html> ...
- flex 垂直居中、两列对齐、自适应宽
flex 垂直居中 <div id="parent"> <div id="child"> </div> </div&g ...
随机推荐
- 【python】lxml中多个xml采用相同节点时出现的问题
今天突然发现了一个lxml的坑. 假设我们有一个节点 <id>123</id> 有两个父节点都要用上述节点,则必须把上面的节点写两遍!用同一个会出错! 出错例子: #!/usr ...
- 【mysql】利用Navicat for MySQL的使用
1. 查看sql语句 如果忘记了某个SQL语句怎么写,可以利用Navicat for MySQL的历史日志来查看 在Navicat for MySQL中,直接对数据库进行想要的操作,然后点击工具-&g ...
- win7自动登录桌面
control userpasswords2 在开始菜单中搜索“运行”,回车打开,或者Win+R打开运行窗口. 键入“control userpasswords2”或者“rundll32 netplw ...
- Roadblocks(poj 3255)
题意:给出n个点,m条双向边,求严格次短路. /* 先spfa预处理出起点到每个点的和每个点到终点的最短距离,然后枚举每条边(这条边必须走),计算此时的最短路径,得出严格次短路. 正确性:因为对于一条 ...
- 数据结构One_Vector(向量的简单实现)
#include <iostream> using namespace std; template<typename Object> class Vector { privat ...
- 转载_虚拟机下LInux(终端)配置网络的方法
出自: http://www.360doc.com/content/14/1027/11/17496895_420258403.shtml 对文章的重点进行剪贴,方便查看. 这几天在虚拟机vmware ...
- poj2236(并查集)
题目链接: http://poj.org/problem?id=2236 题意: 有n台计算机, 已知每台计算机的坐标, 初始时所有计算机都是坏的, 然后修复其中一些计算机, 已修复的计算机距离不超过 ...
- 对象映射组件Tiny Mapper
1.Tiny Mapper的简单实用例子 using System; using System.Collections.Generic; using System.Linq; using System ...
- C#关键字params
using System; using System.Threading; namespace Test { /// <summary> /// params用法: 1.用来修饰方法的参数 ...
- C#的初始化器
using System; using System.Collections; using System.Collections.Generic; using System.IO; using Sys ...