在应用程序中,我正在格式化用户输入
希望SelectionStart始终在最后。
如果添加了字符,则无法使SelectionStart结束
如果删除了字符,那么SelectionStart将结束。
它只会在集合之前转到TextBox的长度;
使用此示例代码,插入符号将位于y之前 - 而不是之后。
<TextBox x:Name="tbUserQuery" Grid.Row="1" Grid.Column="0"
Text="{Binding Path=UserQuery,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"/>
public string UserQuery
{
get
{
// userQuery == aay but what is in the TestBox is aa
// the SelectionStart = 3 (last pos)
tbUserQuery.SelectionStart = userQuery.Length;
tbUserQuery.SelectionLength = 0;
return userQuery;
// now TextBox is aay but SelectionStart is is 2 (not 3)
}
set
{
userQuery = value + "y";
// in the real application adding and removing characters
NotifyPropertyChanged(UserQuery);
return;
}
}