2011-07-29 1 views
1

사용자가 텍스트를 편집 할 수있는 여러 줄의 텍스트 상자가 있습니다. 상태 표시 줄에 현재 줄/문자 인덱스를 표시하고 싶습니다. CaretIndex를 얻을 수 있고 GetLineIndexFromCharacterIndex를 사용하여 라인 인덱스를 가져올 수 있습니다.여러 줄 텍스트 상자 : 현재 줄/문자 색인을 상태 표시 줄에 표시

하지만 상태 표시 줄에 어떻게 바인딩할까요?

+0

안녕하세요. 여기 같은 질문. http://stackoverflow.com/questions/8988539/richtextbox-selectionstart-returns-wrong-index – Nasenbaer

답변

0
RichTextBox rtb = new RichTextBox(); 
int offset = 0; 
rtb.CaretPosition.GetOffsetToPosition(rtb.Document.ContentStart); 
rtb.CaretPosition.GetPositionAtOffset(offset).GetCharacterRect(LogicalDirection.Forward); 
0

IMHO는 간단하고 신뢰할 수있는 방법은 CarteIndex과 DispatcherTimer를 사용하여 GetLineIndexFromCharacterIndex 구성원 모두를 샘플링하는 것입니다. 그런 다음 상태 표시 줄 바인딩을 위해 몇 개의 DP에 값을 표시합니다.

2

첨부 된 동작을 사용하고 싶습니다. 이 동작은 SelectionChanged을 사용하여 변경 사항을 수신하고 첨부 된 두 속성 CaretIndexLineIndex을 적절하게 업데이트 할 수 있습니다.

<TextBox Name="textBox" 
     AcceptsReturn="True" 
     local:CaretBehavior.ObserveCaret="True"/> 

<TextBlock Text="{Binding ElementName=textBox, 
          Path=(local:CaretBehavior.LineIndex)}"/> 
<TextBlock Text="{Binding ElementName=textBox, 
          Path=(local:CaretBehavior.CaretIndex)}"/> 

CaretBehavior

public static class CaretBehavior 
{ 
    public static readonly DependencyProperty ObserveCaretProperty = 
     DependencyProperty.RegisterAttached 
     (
      "ObserveCaret", 
      typeof(bool), 
      typeof(CaretBehavior), 
      new UIPropertyMetadata(false, OnObserveCaretPropertyChanged) 
     ); 
    public static bool GetObserveCaret(DependencyObject obj) 
    { 
     return (bool)obj.GetValue(ObserveCaretProperty); 
    } 
    public static void SetObserveCaret(DependencyObject obj, bool value) 
    { 
     obj.SetValue(ObserveCaretProperty, value); 
    } 
    private static void OnObserveCaretPropertyChanged(DependencyObject dpo, 
                DependencyPropertyChangedEventArgs e) 
    { 
     TextBox textBox = dpo as TextBox; 
     if (textBox != null) 
     { 
      if ((bool)e.NewValue == true) 
      { 
       textBox.SelectionChanged += textBox_SelectionChanged; 
      } 
      else 
      { 
       textBox.SelectionChanged -= textBox_SelectionChanged; 
      } 
     } 
    } 

    static void textBox_SelectionChanged(object sender, RoutedEventArgs e) 
    { 
     TextBox textBox = sender as TextBox; 
     int caretIndex = textBox.CaretIndex; 
     SetCaretIndex(textBox, caretIndex); 
     SetLineIndex(textBox, textBox.GetLineIndexFromCharacterIndex(caretIndex)); 
    } 

    private static readonly DependencyProperty CaretIndexProperty = 
     DependencyProperty.RegisterAttached("CaretIndex", typeof(int), typeof(CaretBehavior)); 
    public static void SetCaretIndex(DependencyObject element, int value) 
    { 
     element.SetValue(CaretIndexProperty, value); 
    } 
    public static int GetCaretIndex(DependencyObject element) 
    { 
     return (int)element.GetValue(CaretIndexProperty); 
    } 

    private static readonly DependencyProperty LineIndexProperty = 
     DependencyProperty.RegisterAttached("LineIndex", typeof(int), typeof(CaretBehavior)); 
    public static void SetLineIndex(DependencyObject element, int value) 
    { 
     element.SetValue(LineIndexProperty, value); 
    } 
    public static int GetLineIndex(DependencyObject element) 
    { 
     return (int)element.GetValue(LineIndexProperty); 
    } 
}