Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在WPF中失去焦点时获取插入符号位置_C#_Wpf_User Interface - Fatal编程技术网

C# 在WPF中失去焦点时获取插入符号位置

C# 在WPF中失去焦点时获取插入符号位置,c#,wpf,user-interface,C#,Wpf,User Interface,我有两个文本框。我为文本框的“onLostFocus”设置了事件设置。如果我把一个单词一个接一个地输入到两个框中,一切都很好。在我点击第一个文本框的时候,我想点击单词的一半(这对于用户来说是完全合理的)。当onLostFocus事件触发时,以下是我的代码: void tbox_LostFocus(object sender, RoutedEventArgs e) { IInputElement focusedelement = FocusManager.GetFoc

我有两个文本框。我为文本框的“onLostFocus”设置了事件设置。如果我把一个单词一个接一个地输入到两个框中,一切都很好。在我点击第一个文本框的时候,我想点击单词的一半(这对于用户来说是完全合理的)。当onLostFocus事件触发时,以下是我的代码:

void tbox_LostFocus(object sender, RoutedEventArgs e)
      {
         IInputElement focusedelement = FocusManager.GetFocusedElement(this);
         if (focusedelement is TextBox)
         {
            TextBox focusedTextBox = focusedelement as TextBox;
            int focusIndex = m_commandsStacker.Children.IndexOf(focusedTextBox);
            int caretIndex = focusedTextBox.CaretIndex;

调用focusedTextBox.CaretIndex时,插入符号索引返回为0。此时,我需要刷新整个表单,设置参数和所有其他类型的whizzery,存储texbox索引和插入符号位置,使所有内容恢复原状。为什么它返回0,而不是插入符号应该位于单词中间的位置?

这在c#express edition 2008(无论是什么版本的WPF)中都不起作用,所以这里最好切换到onTextChanged并在那里运行完全相同的代码,这很有效。显然,要比在失去焦点的情况下运行代码多运行几次,这很烦人。当我有时间的时候,我会检查它是否在c#express 2010中工作,因为我们正在升级(最终)

我刚刚用WPF 3.5 SP1尝试过它:CaretIndex总是返回聚焦文本框的正确插入符号位置。仅当文本框第一次被聚焦时,才会返回0。仅限于此版本:(