将插入符/光标位置设置为字符串值WPF文本框的末尾

Zam*_*oni 69 wpf textbox cursor caret cursor-position

当我第一次打开窗口时,我尝试将插入符/光标位置设置为WPF文本框中字符串值的末尾.当我的窗口打开时,我使用FocusManager在我的文本框上设置焦点.

似乎没什么用.有任何想法吗?

注意,我使用的是MVVM模式,我只从代码中包含了一部分XAML.

<Window 
    FocusManager.FocusedElement="{Binding ElementName=NumberOfDigits}"
    Height="400" Width="800">

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <TextBox Grid.Column="0" Grid.Row="0" 
                 x:Name="NumberOfDigits"
                 IsReadOnly="{Binding Path=IsRunning, Mode=TwoWay}"
                 VerticalContentAlignment="Center"
                 Text="{Binding Path=Digits, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        <Button Grid.Column="0" Grid.Row="1" 
                 Margin="10,0,10,0"
                 IsDefault="True"
                 Content="Start" 
                 Command="{Binding StartCommand}"/>
    </Grid>
 </Window>
Run Code Online (Sandbox Code Playgroud)

wpf*_*abe 93

您可以使用a的CaretIndex属性设置插入位置TextBox.请记住,这不是一个DependencyProperty.不过,您仍然可以像这样在XAML中设置它:

<TextBox Text="123" CaretIndex="{x:Static System:Int32.MaxValue}" />
Run Code Online (Sandbox Code Playgroud)

请记住设置属性CaretIndex Text,否则它将无法正常工作.因此,如果Text在示例中绑定到like,它可能无法工作.在这种情况下,只需使用这样的代码隐藏.

NumberOfDigits.CaretIndex = NumberOfDigits.Text.Length;
Run Code Online (Sandbox Code Playgroud)

  • 是的,我试图绑定到CaretIndex并且失败了.在Window Loaded Event中将代码添加到代码隐藏效果非常好.谢谢. (2认同)

Lou*_*uis 20

您还可以创建一个行为,虽然仍然是代码隐藏,但具有可重用的优势.

使用文本框的焦点事件的简单行为类的示例:

class PutCursorAtEndTextBoxBehavior: Behavior<UIElement>
{
   private TextBox _textBox;

   protected override void OnAttached()
   {
        base.OnAttached();

        _textBox = AssociatedObject as TextBox;

        if (_textBox == null)
        {
            return;
        }
        _textBox.GotFocus += TextBoxGotFocus;
   }

    protected override void OnDetaching()
    {
        if (_textBox == null)
        {
            return;
        }
        _textBox.GotFocus -= TextBoxGotFocus;

        base.OnDetaching();
    }

    private void TextBoxGotFocus(object sender, RoutedEventArgs routedEventArgs)
    {
        _textBox.CaretIndex = _textBox.Text.Length;
    }
}    
Run Code Online (Sandbox Code Playgroud)

然后,在您的XAML中,您附加了这样的行为:

    <TextBox x:Name="MyTextBox" Text="{Binding Value}">
        <i:Interaction.Behaviors>
            <behaviors:PutCursorAtEndTextBoxBehavior/>
        </i:Interaction.Behaviors>
    </TextBox>
Run Code Online (Sandbox Code Playgroud)


Mic*_*ski 5

这对我有用。我也在使用 MVVM 模式。但是,我使用 MMVM 的目的是使单元测试成为可能,并使更新我的 UI(松散耦合)更容易。我没有看到自己对光标的位置进行单元测试,所以我不介意使用背后的代码来完成这个简单的任务。

    public ExpeditingLogView()
    {
        InitializeComponent();

        this.Loaded += (sender, args) =>
        {                                
            Description.CaretIndex = Description.Text.Length;
            Description.ScrollToEnd();
            Description.Focus();
        };
    }
Run Code Online (Sandbox Code Playgroud)