0

有什么方法可以按列显示我的文本,例如:

1   5
2   6
3   7
4   8

像这样:

<TextBox x:Name="mytextbox" TextWrapping="Wrap" AcceptsReturn="True" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" />

这是我的TextBox。我从一个字符串服务中获取文本,并在这个文本框中设置得很好,这不是问题。问题是我如何在上面提到的列中显示多个文本?

4

3 回答 3

1
<TextBox x:Name="Mytextbox" TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto"
         Text="1&#x09;5&#x0d;&#x0a;2&#x09;6&#x0d;&#x0a;3&#x09;7&#x0d;&#x0a;4&#x09;8">
</TextBox>

您也可以在代码中指定文本,如示例中所示:

Mytextbox.Text = "1\t3" + Environment.NewLine + "2\t4";
于 2013-07-16T05:52:05.220 回答
1

@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;
            }
        }
    }
}
于 2013-07-16T07:58:16.390 回答
0

文本框中有 AcceptsTab 布尔属性允许您在字符串中使用选项卡,当您想输入文本框并按 Tab 键时,文本框焦点不会丢失,并且在文本中出现选项卡空间并在将其保存到数据库后或变量并将其分配给您拥有标签的文本框。

<TextBox TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Auto" Height="70"/>
于 2016-01-15T20:22:07.297 回答