使用WrapPanel每行项目数量指定(Specifying number of items per row using a WrapPanel)
I'm attempting to create an application that looks much like the Windows 8 Metro UI with the tiles.. right now if you click on a tile I have an animation that increases the width and reveals more info on that tile.
I have the tiles laid out in a WrapPanel which is nice because if I resize the tile the other tiles next to it move and keep it's margin perfectly. However I've been researching this for awhile, I would like to if possible limit the number of items in the wrap panel to two wide (Right now it's three wide) as if you select one of the tiles it resizes itself and pushes a tile next to it (or if it's the end tile it will push itself) to the next row, while my animations are smooth it doesn't look the best presentation-wise..
Could someone point me towards how I might specify my wrappanel to only have a width of two items across?
Any help is very much appreciated.
解决方案
Try making your own wrap panel deriving from standard wrap panel as described in this post in detail. Post addresses the same issue which you are trying to solve.
public class MyWrapPanel : WrapPanel { public int MaxRows { get { return (int)GetValue(MaxRowsProperty); } set { SetValue(MaxRowsProperty, value); } } public static readonly DependencyProperty MaxRowsProperty = DependencyProperty.Register("MaxRows", typeof(int), typeof(MyWrapPanel), new UIPropertyMetadata(4)); protected override Size ArrangeOverride(Size finalSize) { Point currentPosition = new Point(); double ItemMaxHeight = 0.0; int RowIndex = 0; foreach (UIElement child in Children) { ItemMaxHeight = ItemMaxHeight > child.DesiredSize.Height ? ItemMaxHeight : child.DesiredSize.Height; if (currentPosition.X + child.DesiredSize.Width > this.DesiredSize.Width) { currentPosition = new Point(0, currentPosition.Y + ItemMaxHeight); ItemMaxHeight = 0.0; RowIndex++; } if (RowIndex < MaxRows) { child.Visibility = System.Windows.Visibility.Visible; Rect childRect = new Rect(currentPosition, child.DesiredSize); child.Arrange(childRect); } else { Rect childRect = new Rect(currentPosition, new Size(0,0)); child.Arrange(childRect); } currentPosition.Offset(child.DesiredSize.Width, 0); } return finalSize; } protected override Size MeasureOverride(Size availableSize) { return base.MeasureOverride(availableSize); } }
我试图如果你点击一个瓷砖我有增加的宽度和揭示了的瓷砖更多信息动画创建一个应用程序,它看起来很像用瓦片在Windows 8的Metro UI ..现在。
我有一个WrapPanel这是很好的奠定了地砖,因为如果我调整瓷砖瓷砖等它旁边移动,并保持它的利润率非常完美。不过,我一直在研究这个一段时间,我想如果可能的话限制在画布面板项目,以两个宽数(现在它是广三),如果你选择瓷砖的一个它自身的大小和推动瓷砖旁边(或者如果它是结束瓷砖它会推动自身)的下一行,而我的动画是光滑的它看起来并不最好的表现明智的。
有人能指出我朝着我怎么可以指定我wrappanel只能有两个项目在整个宽度是多少?
任何帮助是非常赞赏。
解决方案
试着让你的自己包裹面板从标准画布面板派生作为的这个帖子的细节。 。邮政地址,你正在试图解决同样的问题。
公共类MyWrapPanel:WrapPanel { 公众诠释MAXROWS { {返回(INT)的GetValue(MaxRowsProperty); } 集合{的SetValue(MaxRowsProperty,值); } } 公共静态只读的DependencyProperty MaxRowsProperty = DependencyProperty.Register(MAXROWS的typeof(INT)的typeof(MyWrapPanel),新UIPropertyMetadata(4)); 保护覆盖面积ArrangeOverride(尺寸finalSize) { 点currentPosition =新点(); 双ItemMaxHeight = 0.0; INT的rowIndex = 0; 的foreach(儿童的UIElement儿童) { ItemMaxHeight = ItemMaxHeight> child.DesiredSize.Height? ItemMaxHeight:child.DesiredSize.Height; 如果(currentPosition.X + child.DesiredSize.Width> this.DesiredSize.Width) { currentPosition =新的点(0,+ currentPosition.Y ItemMaxHeight); ItemMaxHeight = 0.0; 的rowIndex ++; } 如果(rowIndex位置< MAXROWS) { child.Visibility = System.Windows.Visibility.Visible; 矩形childRect =新的矩形(currentPosition,child.DesiredSize); child.Arrange(childRect); } ,否则 { 矩形childRect =新的矩形(currentPosition,新尺寸(0,0)); child.Arrange(childRect); } currentPosition.Offset(child.DesiredSize.Width,0); } 返回finalSize; } 保护覆盖面积的MeasureOverride(尺寸availableSize) { 返回base.MeasureOverride(availableSize); } }