2011-07-29 17 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

私見では、最も簡単かつ信頼性の高い方法はCarteIndexとDispatcherTimerを使用してGetLineIndexFromCharacterIndexメンバーの両方をサンプリングすることです。次に、ステータスバーのバインディングの値を2つのDPに公開します。

2

私はそれに付随する動作を使用したいと思います。この動作は、SelectionChangedで変更をリッスンし、添付された2つのプロパティCaretIndexおよびLineIndexをそれに応じて更新できます。

<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); 
    } 
} 
関連する問題