代码之家  ›  专栏  ›  技术社区  ›  Zamboni

将插入符号/光标位置设置为字符串值wpf文本框的结尾

  •  60
  • Zamboni  · 技术社区  · 15 年前

    我尝试将插入符号/光标位置设置为 结束 当我第一次打开窗口时,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>
    
    5 回复  |  直到 8 年前
        1
  •  85
  •   wpfwannabe    15 年前

    可以使用设置插入符号位置 CaretIndex A的性质 TextBox . 请记住,这不是 DependencyProperty . 不过,您仍然可以这样在XAML中设置它:

    <TextBox Text="123" CaretIndex="{x:Static System:Int32.MaxValue}" />
    

    请记住设置 看护指数 之后 Text 否则它将不起作用。因此,如果你绑定到 文本 就像你的例子。在这种情况下,只需像这样使用代码隐藏。

    NumberOfDigits.CaretIndex = NumberOfDigits.Text.Length;
    
        2
  •  20
  •   Louis    9 年前

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

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

    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;
        }
    }    
    

    然后,在XAML中,您附加如下行为:

        <TextBox x:Name="MyTextBox" Text="{Binding Value}">
            <i:Interaction.Behaviors>
                <behaviors:PutCursorAtEndTextBoxBehavior/>
            </i:Interaction.Behaviors>
        </TextBox>
    
        3
  •  4
  •   Michael Ceranski    8 年前

    这对我有用。我也在使用MVVM模式。但是,我使用mmvm的目的是使单元测试成为可能,并使更新UI(松散耦合)变得更容易。我不认为自己在单元测试光标的位置,所以我不介意在这个简单的任务中使用后面的代码。

        public ExpeditingLogView()
        {
            InitializeComponent();
    
            this.Loaded += (sender, args) =>
            {                                
                Description.CaretIndex = Description.Text.Length;
                Description.ScrollToEnd();
                Description.Focus();
            };
        }
    
        4
  •  3
  •   Horacio Rodriguez molbalga    8 年前

    如果您的文本框(WinForms)是带有垂直滚动条的多行文本框,则可以尝试此操作:

    textbox1.Select(textbox1.Text.Length-1, 1);
    textbox1.ScrollToCaret();
    

    注意:在wpf.ScrollToCaret()中,不是文本框的成员。

        5
  •  1
  •   andreikashin    9 年前

    如果是多行 TextBox 设置光标不够。 试试这个:

    NumberOfDigits.ScrollToEnd();
    
    推荐文章