在WPF中的文本框中显示文本列

时间:2013-07-16 05:26:47

标签: wpf textbox

有没有什么方法可以在列中明确显示我的文字:

1   5
2   6
3   7
4   8

像这样:

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

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

3 个答案:

答案 0 :(得分: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";

答案 1 :(得分:1)

@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;
            }
        }
    }
}

答案 2 :(得分:0)

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

<TextBox TextWrapping="Wrap" AcceptsReturn="True" AcceptsTab="True" VerticalScrollBarVisibility="Auto" Height="70"/>