我对Uniform网格中的布局有要求。下面是布局
在Uniform网格中,第二行布局项应从Right_to_Left(不使用Flow方向)开始。目前它是从Left_to_Right
开始的当我折叠“Button1”时,布局应如下所示
任何人都可以帮助我
答案 0 :(得分:2)
猜猜你可以通过调整ArrangeOverride(...)
UniformGrid
来做到这一点
如果您只是在寻找样品,可以下载演示 - > Here
首先,我找到了一个从Here
以垂直方向(列方式)布置元素的示例因此,通过该解决方案,我可以将ArrangeOverride(...)
更新为:
protected override Size ArrangeOverride(Size arrangeSize) {
var finalRect = new Rect(0.0, 0.0, arrangeSize.Width / _columns, arrangeSize.Height / _rows);
Double width = finalRect.Width;
Double maxWidth = arrangeSize.Width;
finalRect.X += finalRect.Width * FirstColumn;
int currentRow = 1;
foreach (UIElement element in InternalChildren) {
element.Arrange(finalRect);
if (element.Visibility == Visibility.Collapsed)
continue;
if (currentRow % 2 == 0) {
finalRect.X -= width;
if (finalRect.X <= -width) {
++currentRow;
finalRect.Y += finalRect.Height;
finalRect.X = 0;
}
} else {
finalRect.X += width;
if (finalRect.X >= maxWidth) {
++currentRow;
finalRect.Y += finalRect.Height;
finalRect.X = maxWidth - width;
}
}
}
return arrangeSize;
}
<强>逻辑:强>
我们几乎在foreach(...)
里面检查元素的当前行和每个替换行,通过相应地修改它的X偏移来切换它的水平方向。
这会获得您正在寻找的行为。
以下是msdn链接将来消失的完整派生类代码:
public class MyUniformGrid : UniformGrid {
private int _columns;
private int _rows;
protected override Size MeasureOverride(Size constraint) {
UpdateComputedValues();
var availableSize = new Size(constraint.Width / (_columns), constraint.Height / (_rows));
double width = 0.0;
double height = 0.0;
int num3 = 0;
int count = base.InternalChildren.Count;
while (num3 < count) {
UIElement element = base.InternalChildren[num3];
element.Measure(availableSize);
Size desiredSize = element.DesiredSize;
if (width < desiredSize.Width) {
width = desiredSize.Width;
}
if (height < desiredSize.Height) {
height = desiredSize.Height;
}
num3++;
}
return new Size(width * _columns, height * _rows);
}
private void UpdateComputedValues() {
_columns = Columns;
_rows = Rows;
if (FirstColumn >= _columns) {
FirstColumn = 0;
}
if (FirstColumn > 0)
throw new NotImplementedException("There is no support for seting the FirstColumn (nor the FirstRow).");
if ((_rows == 0) || (_columns == 0)) {
int num = 0; // Visible children
int num2 = 0;
int count = base.InternalChildren.Count;
while (num2 < count) {
UIElement element = base.InternalChildren[num2];
if (element.Visibility != Visibility.Collapsed) {
num++;
}
num2++;
}
if (num == 0) {
num = 1;
}
if (_rows == 0) {
if (_columns > 0) {
_rows = ((num + FirstColumn) + (_columns - 1)) / _columns;
} else {
_rows = (int)Math.Sqrt(num);
if ((_rows * _rows) < num) {
_rows++;
}
_columns = _rows;
}
} else if (_columns == 0) {
_columns = (num + (_rows - 1)) / _rows;
}
}
}
protected override Size ArrangeOverride(Size arrangeSize) {
var finalRect = new Rect(0.0, 0.0, arrangeSize.Width / _columns, arrangeSize.Height / _rows);
Double width = finalRect.Width;
Double maxWidth = arrangeSize.Width;
finalRect.X += finalRect.Width * FirstColumn;
int currentRow = 1;
foreach (UIElement element in InternalChildren) {
element.Arrange(finalRect);
if (element.Visibility == Visibility.Collapsed)
continue;
if (currentRow % 2 == 0) {
finalRect.X -= width;
if (finalRect.X <= -width) {
++currentRow;
finalRect.Y += finalRect.Height;
finalRect.X = 0;
}
} else {
finalRect.X += width;
if (finalRect.X >= maxWidth) {
++currentRow;
finalRect.Y += finalRect.Height;
finalRect.X = maxWidth - width;
}
}
}
return arrangeSize;
}
}
注意:强>
此实现目前不适用于UniformGrid.FirstColumn
属性。如果你需要ofc,你可以调整覆盖范围以适应这种情况。