XAML:自动在文本框中移动光标

时间:2013-01-15 11:31:00

标签: wpf xaml

我不知道这是否有效但是有没有机会将Textbox内的光标自动移动到一个位置(1个空格键单击)。例如: 我运行应用程序,Textbox内的光标自动移动一个空格键

我的文本框:

<TextBox TextChanged="Searchbox_TextChanged" x:Name="Testing" Margin="2" Grid.Row="1" Text="{Binding SearchSmthg, Mode=TwoWay,  UpdateSourceTrigger=PropertyChanged}" VerticalContentAlignment="Center" Controls:TextboxHelper.ClearTextButton="True" Controls:TextboxHelper.Watermark="Search ..."/>

1 个答案:

答案 0 :(得分:0)

使用CaretIndexFocusManager

  <Window x:Class="WpfApplication1.Window1"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="Window1" Height="300" Width="300">
        <Grid FocusManager.FocusedElement="{Binding ElementName=Focustext}">
            <TextBox x:Name="Focustext" Text=" " CaretIndex="1" MaxHeight="25"  />
        </Grid>
    </Window>

修改:添加GotFocus事件处理程序并在其中设置CaretIndex=1

XAML:

<TextBox GotFocus="Testing_GotFocus"

代码背后:

private void Testing_GotFocus(object sender, RoutedEventArgs e)
        {
            //make sure your Textbox is not empty..
            Testing.CaretIndex = 1;
        }