@omeriqbal:解释我的评论。从您的字符串创建一个集合
this.MyText = new List<string>{"1","2","3","4",...};
而不是文本框,您可以使用项目控件
<ItemsControl itemsSource="{Binding MyText}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Local:UniformGrid2 Orientation="Vertical" Rows="6" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
如果你想要一个修复行大小,你可以创建你自己的 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;
}
}
}
}