@omeriqbal:解釋一下我的意見。從您的字符串
this.MyText = new List<string>{"1","2","3","4",...};
代替文本框,如果你想有一個修復你可以使用一個ItemsControl
<ItemsControl itemsSource="{Binding MyText}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Local:UniformGrid2 Orientation="Vertical" Rows="6" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
創建集合ROWSIZE你可以創建你自己的uniformgrid
public class UniformGrid2 : UniformGrid
{
private int _columns;
private int _rows;
#region Orientation
/// <summary>
/// Orientation Dependency Property
/// </summary>
public static readonly DependencyProperty OrientationProperty =
StackPanel.OrientationProperty.AddOwner(typeof(UniformGrid2),
new FrameworkPropertyMetadata((Orientation)Orientation.Horizontal,
FrameworkPropertyMetadataOptions.AffectsMeasure));
/// <summary>
/// Gets or sets the Orientation property.
/// </summary>
public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
#endregion
protected override Size ArrangeOverride(Size arrangeSize)
{
if (Orientation == Orientation.Horizontal)
return base.ArrangeOverride(arrangeSize);
else
return ArrangeOverrideVertical(arrangeSize);
}
protected override Size MeasureOverride(Size constraint)
{
if (Orientation == Orientation.Horizontal)
return base.MeasureOverride(constraint);
else
return MeasureOverrideVertical(constraint);
}
private Size ArrangeOverrideVertical(Size arrangeSize)
{
Rect finalRect = new Rect(0.0, 0.0, arrangeSize.Width/((double)_columns), arrangeSize.Height/((double)_rows));
double height = finalRect.Height;
double totalHeight = arrangeSize.Height - 1.0;
foreach (UIElement element in base.InternalChildren)
{
element.Arrange(finalRect);
if (element.Visibility != Visibility.Collapsed)
{
finalRect.Y += height;
if (finalRect.Y >= totalHeight)
{
finalRect.X += finalRect.Width;
finalRect.Y = 0.0;
}
}
}
return arrangeSize;
}
private Size MeasureOverrideVertical(Size constraint)
{
UpdateComputedValuesVertical();
Size availableSize = new Size(constraint.Width/((double)_columns), constraint.Height/((double)_rows));
double width = 0.0;
double height = 0.0;
int i = 0;
int count = base.InternalChildren.Count;
while (i < count)
{
UIElement element = base.InternalChildren[i];
element.Measure(availableSize);
Size desiredSize = element.DesiredSize;
if (width < desiredSize.Width)
{
width = desiredSize.Width;
}
if (height < desiredSize.Height)
{
height = desiredSize.Height;
}
i++;
}
return new Size(width * _columns, height * _rows);
}
private void UpdateComputedValuesVertical()
{
_columns = Columns;
_rows = Rows;
// Ignore FirstColumn property
FirstColumn = 0;
if (_rows == 0 || _columns == 0)
{
int visibleChildren = 0;
int i = 0;
int count = base.InternalChildren.Count;
while (i < count)
{
UIElement element = base.InternalChildren[i];
if (element.Visibility != Visibility.Collapsed)
{
visibleChildren++;
}
i++;
}
if (visibleChildren == 0)
{
visibleChildren = 1;
}
if (_columns == 0)
{
if (_rows > 0)
{
_columns = (visibleChildren + (_rows - 1))/_rows;
}
else
{
_columns = (int)Math.Sqrt((double)visibleChildren);
if ((_columns * _columns) < visibleChildren)
{
_columns++;
}
_rows = _columns;
}
}
else if (_rows == 0)
{
_rows = (visibleChildren + (_columns - 1))/_columns;
}
}
}
}
如果你想包一個集合在你的文字,你可以這樣做很容易用一個ItemsControl和ItemsPanel權如wrappanel – blindmeis
對不起blindmeis我因此未得到你的答案... – omeriqbal