2014-08-27 1 views
1

TextWrapping = "Wrap"속성을 사용하여 WPF 텍스트 블록을 설정했습니다.wpf 텍스트 블록 컨트롤에서 래핑 할 때 들여 쓰기 유지

처음에는 탭 문자 (내 경우에는 vbTab)를 사용하여 긴 문자열을 전달할 때 이것을 감안하고 문자열의 들여 쓰기 부분을 들여 쓰는 것이 좋습니다. 대신에 예를 들어 :

[vbTab]

thisisreallylong는

제가

는 [vbTab]

을 andwrapped thisisreallylong

[vbTab]

원하는 andwrapped

이상적으로는 여러 개의 탭 등에 대해서도 마찬가지입니다.

[편집 - 추가 정보] TextBlock의 변수 크기의 수 및 들여 쓰기의 다양한 양으로 여러 줄의 텍스트를 포함하기 때문에, 난 그냥 여유를 가지고 또는 수동으로 할 수

이 문자열을 분할 탭을 추가하십시오.

본질적으로 내가 원하는 것은 단락과 같은 텍스트 줄을 처리하고 줄 바꿈을 할 때 들여 쓰기를 유지하는 것입니다. 당신의 아이디어를 바탕으로

+0

을 시도, XML'때리는 수 '당신이 찾고있는 비주얼을 완성 할 수 있을까요? –

+0

문제는 탭을 유지하는 것이 아니라 제대로 표시되는 것입니다. 텍스트가 여러 줄로 줄을 감쌀 때 줄을 들여 씁니다. 현재 줄 바꿈 텍스트의 두 번째 줄 (등)은 왼쪽 가장자리에 다시 붙습니다. – simonalexander2005

+0

여기에 여백이 필요할 수 있습니다. 탭을 실행하지 못할 수도 있습니다. – pushpraj

답변

4

, 나는

I는 각각 0.5 인치의 여백에 모든 라인의 시작 부분에있는 모든 탭을 변환 할 수 있습니다이 솔루션을 마련 할 수 있어요 및 단락에 동일한 텍스트를 추가합니다 계산 된 여백을 동일하게 적용

실행 굵게, 인라인 UI 컨테이너 등과 같은 기본 텍스트 인라인에 유용 할만큼 TextBlock을 사용할 수 없었습니다. TextBlock에서 단락을 추가하는 것이 더 복잡해 졌으므로 해결책을 만들었습니다. FlowDocument를 기반으로합니다.

결과

예를 아래에

result

는 동일한 첨부 할 수 있도록 FlowDocumentScrollViewer 또는 RichTextBox 또는 FlowDocumentReader 또는 일반 FlowDocument

내가 연결된 속성을 사용하여 솔루션을 만들었을 사용하여 동일한 보여 언급 된 것 또는 문서의 호스트를 추가 할 수 있습니다. IndentationProvider.Text을 원하는 호스트로 설정하기 만하면됩니다.

XAML

<Window x:Class="MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:sys="clr-namespace:System;assembly=mscorlib" 
     xmlns:l="clr-namespace:PreservingIndentationDemo" 
     Title="MainWindow" 
     Height="350" 
     Width="525"> 
    <Window.Resources> 
     <sys:String x:Key="longString" 
        xml:space="preserve">&#x09;this is really long and wrapped 

&#x09;&#x09;another line this is also really long and wrapped 

&#x09;one more line this is also really long and wrapped 

another line this is also really long and wrapped 

&#x09;&#x09;another line this is also really long and wrapped 
     </sys:String> 
    </Window.Resources> 
    <Grid> 
     <FlowDocumentScrollViewer l:IndentationProvider.Text="{StaticResource longString}" /> 
     <!--<RichTextBox l:TextToParaHelper.Text="{StaticResource longString}" IsReadOnly="True"/>--> 
     <!--<FlowDocumentReader l:TextToParaHelper.Text="{StaticResource longString}" />--> 
     <!--<FlowDocument l:TextToParaHelper.Text="{StaticResource longString}" />--> 
    </Grid> 
</Window> 

&#x09; 탭 숯불 지칭

IndentationProvider

Class IndentationProvider 

    Public Shared Function GetText(obj As DependencyObject) As String 
     Return DirectCast(obj.GetValue(TextProperty), String) 
    End Function 

    Public Shared Sub SetText(obj As DependencyObject, value As String) 
     obj.SetValue(TextProperty, value) 
    End Sub 

    ' Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc... 
    Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.RegisterAttached("Text", GetType(String), GetType(IndentationProvider), New PropertyMetadata(Nothing, AddressOf OnTextChanged)) 

    Private Shared Sub OnTextChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs) 
     Dim blocks As BlockCollection = Nothing 

     Dim rtb As RichTextBox = TryCast(d, RichTextBox) 
     If rtb IsNot Nothing Then 
      rtb.Document.Blocks.Clear() 
      blocks = rtb.Document.Blocks 
     End If 

     If blocks Is Nothing Then 
      Dim fd As FlowDocument = TryCast(d, FlowDocument) 
      If fd IsNot Nothing Then 
       fd.Blocks.Clear() 
       blocks = fd.Blocks 
      End If 
     End If 

     If blocks Is Nothing Then 
      Dim fdr As FlowDocumentReader = TryCast(d, FlowDocumentReader) 
      If fdr IsNot Nothing Then 
       fdr.Document = New FlowDocument() 
       blocks = fdr.Document.Blocks 
      End If 
     End If 

     If blocks Is Nothing Then 
      Dim fdr As FlowDocumentScrollViewer = TryCast(d, FlowDocumentScrollViewer) 
      If fdr IsNot Nothing Then 
       fdr.Document = New FlowDocument() 
       blocks = fdr.Document.Blocks 
      End If 
     End If 

     Dim newValue As String = TryCast(e.NewValue, String) 
     If Not String.IsNullOrWhiteSpace(newValue) Then 
      For Each line As String In newValue.Split(ControlChars.Lf) 
       Dim leftMargin As Double = 0 
       Dim newLine As String = line 
       While newLine.Length > 0 AndAlso newLine(0) = ControlChars.Tab 
        leftMargin += 0.5 
        newLine = newLine.Remove(0, 1) 
       End While 
       Dim marginInch As String = leftMargin & "in" 
       Dim marginDip As Double = CDbl(New LengthConverter().ConvertFromString(marginInch)) 

       Dim para As New Paragraph(New Run(newLine)) With {.Margin = New Thickness(marginDip, 0, 0, 0)} 
       blocks.Add(para) 
      Next 
     End If 
    End Sub 
End Class 

데모

,536,913 , 어쩌면 그냥 왼쪽 '여백 그냥 모든 것을 왼쪽 공간을 갖고 싶어, 그렇지 않은 경우, 공간 = 당신의 TextBlock의에` "보존"실제로 문자열에 탭이 있다면 63,210

demo project

+0

고마워요! 당신은 그것에 많은 일을 넣었습니다. – simonalexander2005

+0

도전과 아이디어에 대해 감사 드리며, 새로운 트릭을 배웠습니다. – pushpraj

관련 문제