在放置鼠标指针或键盘(箭头键即->;和<;-)光标时从wPF组合框中拾取值(C#3.0)

在放置鼠标指针或键盘(箭头键即->;和<;-)光标时从wPF组合框中拾取值(C#3.0),wpf,c#-3.0,Wpf,C# 3.0,我有一个WPF组合框 <ComboBox Name="cmbExpression" IsEditable="True"/> 案例2: User put the cursor or mouse pointer on x of f(x). Output will be f(x). User put on v of mv Output: mv User put the cursor on t of sometext. Output: sometext User put th

我有一个WPF组合框

<ComboBox Name="cmbExpression" IsEditable="True"/> 
案例2:

User put the cursor or mouse pointer on x of f(x).

Output will be f(x).
User put on v of mv

Output: mv
User put the cursor on t of sometext.

Output: sometext
User put the cursor on ( on Fx(X)

Output: Fx(X)
    User put the cursor on ',' of  fx(a,b,c) 

   Output: fx(a,b,c)
案例3:

User put the cursor or mouse pointer on x of f(x).

Output will be f(x).
User put on v of mv

Output: mv
User put the cursor on t of sometext.

Output: sometext
User put the cursor on ( on Fx(X)

Output: Fx(X)
    User put the cursor on ',' of  fx(a,b,c) 

   Output: fx(a,b,c)
案例4:

User put the cursor or mouse pointer on x of f(x).

Output will be f(x).
User put on v of mv

Output: mv
User put the cursor on t of sometext.

Output: sometext
User put the cursor on ( on Fx(X)

Output: Fx(X)
    User put the cursor on ',' of  fx(a,b,c) 

   Output: fx(a,b,c)
案例5:

User put the cursor or mouse pointer on x of f(x).

Output will be f(x).
User put on v of mv

Output: mv
User put the cursor on t of sometext.

Output: sometext
User put the cursor on ( on Fx(X)

Output: Fx(X)
    User put the cursor on ',' of  fx(a,b,c) 

   Output: fx(a,b,c)
我正在使用C#3.0和WPF

需要帮助


谢谢

TextBox有一个名为的方法,它返回提供位置的字符索引,TextBlock有一个类似的方法,它返回TextPointer。您可以使用这些方法查找光标下的内容

<ComboBox IsEditable="True" MouseMove="ComboBox_MouseMove" />

代码

private void组合框\u MouseMove(对象发送方,MouseEventArgs e)
{
var combobox=(combobox)发送方;
//可编辑组合框使用名为PART_EditableTextBox的文本框
var textbox=(textbox)combobox.Template.FindName(“PART_EditableTextBox”,combobox);
var pos=textbox.GetCharacterIndexFromPoint(例如GetPosition(textbox),true);
var text=textbox.text;
if(string.IsNullOrEmpty(text))
返回;
txt.Text=GetWordAtPos(Text,pos);
}
私有静态字符串GetWordAtPos(字符串str,int pos)
{
堆栈匹配=新堆栈();
int-wordStart=0,wordEnd=0;
对于(int i=0;ipos)
wordEnd=i;
其他的
wordStart=i;
}
打破
格“(”:
火柴。推(');
打破
案例“)”:
if(matches.Count==0 | | matches.Peek()!=c)
抛出新ArgumentException(“找到”)而不匹配(字符“);
其他的
匹配。Pop();
打破
}
}
字符串字;
if(wordEnd==0)
word=str.Substring(wordStart);
其他的
word=str.Substring(wordStart,wordEnd-wordStart);
返回单词.Trim(',');
}

如果您需要对每个令牌的外观进行更多的控制,您可能需要研究一种不同的方法,即为每个令牌生成一个Run元素,但这似乎适用于您所描述的情况。

对于键盘导航(->&fx(A、b、c)的情况失败)我更新了代码以处理您添加的新案例。如果遇到关闭而没有打开的案例,则会引发错误,我不知道您希望在这种情况下发生什么。我也不确定是否需要处理嵌套函数,这段代码返回最外层的函数。如果情况变得更复杂,您可能需要使用解析器。非常感谢您的及时回复