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

86

我正在尝试在首次打开窗口时将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>
11个回答

0
WPF TextBox.Focus(); TextBox.CaretIndex = TextBox.Text.Length + 1;

这难道不是与被接受的答案完全相同吗?如果不是,请解释一下。 - Klaus Gütter
根据目前的写法,你的回答不够清晰。请编辑以添加更多细节,帮助其他人理解这如何回答所提出的问题。你可以在帮助中心找到关于如何撰写好回答的更多信息。 - Community

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接