2013-07-16 60 views
0

有什麼辦法,我可以展示我在列文本明智喜歡:顯示文本列在WPF

1 5 
2 6 
3 7 
4 8 

像這樣:

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

這是我的TextBox。我從字符串服務中獲取文本,並將其設置在該文本框中,這不是問題。問題是我如何在列明智顯示多個文本,如上所述?

+0

如果你想包一個集合在你的文字,你可以這樣做很容易用一個ItemsControl和ItemsPanel權如wrappanel – blindmeis

+0

對不起blindmeis我因此未得到你的答案... – omeriqbal

回答

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

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

感謝慷慨help..Your交代幫助我...它的工作..... – omeriqbal

+0

費爾自由標記爲答案:) – blindmeis

0

有文本框中的AcceptsTab布爾屬性允許您在字符串中包含選項卡,並且當您要鍵入文本框並按Tab鍵時,文本框焦點不會丟失,並且在文本中出現標籤空間,將它保存到數據庫或變量,並將其分配給您有標籤的文本框。

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