如何在c#wpf中的蒙版文本框中设置默认光标位置 [英] how to set default cursor position in masked textbox in c# wpf

查看:160
本文介绍了如何在c#wpf中的蒙版文本框中设置默认光标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个蒙面文本框,用于以指定格式输入传真号码。我在插入符号起始位置有问题。当我点击框中的最后一个位置时它保持在相同的位置,但是插入符号应该处于起始位置(| + 1- - - ,就像这样)。



i have a masked textbox which is used to enter the fax number in specified format. i have a problem on caret start position in that. when i click on last position in the box it remains in the same position, but the caret should be in the starting position (|+1- - - , like this).

<msk:MaskedTextBox x:Name="txt_Fax" Mask="+1-000-000-0000" PromptChar=" "
     Text="{Binding Path=Fax}"  MouseDown="txt_Fax_MouseDown"  />

  // (Codebehind)
   private void txt_Fax_MouseDown(object sender, MouseButtonEventArgs e)
    {
        txt_Fax.SelectionStart = txt_Fax.MaskedTextProvider.LastAssignedPosition + 1;
        txt_Fax.SelectionLength = 0;
    }





我必须写这个代码?



in which event i have to write this code?

推荐答案

尝试另一个事件,如GotFocus或MouseUp。

当你激活一个控件时,会发生一系列事件,如果过早设置插入符号,则该值将被链中较晚的事件覆盖。





希望这会有所帮助。
try another event like GotFocus or MouseUp.
When you "activate" a control, there are a bunch of events happening, if you set the caret too soon, the value will be overwritten by an event that comes later in the chain.


Hope this helps.


这篇关于如何在c#wpf中的蒙版文本框中设置默认光标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆