问题
I have a multiline textbox where the user can edit text. In a statusbar I'd like to show the current line / character index. I know I can get the CaretIndex, and use GetLineIndexFromCharacterIndex to get the line-index.
But how would I go about binding that into the statusbar ?
回答1:
I'd use an attached behavior for that. The behavior can listens to changes with SelectionChanged
and update two attached properties CaretIndex
and LineIndex
accordingly.
<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);
}
}
回答2:
RichTextBox rtb = new RichTextBox();
int offset = 0;
rtb.CaretPosition.GetOffsetToPosition(rtb.Document.ContentStart);
rtb.CaretPosition.GetPositionAtOffset(offset).GetCharacterRect(LogicalDirection.Forward);
回答3:
IMHO, the simplest and reliable way is to sample both the CarteIndex and the GetLineIndexFromCharacterIndex members using a DispatcherTimer. Then exposing the values on a couple of DP for the status bar bindings.
来源:https://stackoverflow.com/questions/6870365/multiline-textbox-display-current-line-character-index-in-statusbar