C# 如何在RichTextBox中选择文本,同时在另一个文本框中保持键盘焦点
我正在WPF中开发一个聊天窗口,其中有一个Forms.RichTextBoxC# 如何在RichTextBox中选择文本,同时在另一个文本框中保持键盘焦点,c#,wpf,winforms,C#,Wpf,Winforms,我正在WPF中开发一个聊天窗口,其中有一个Forms.RichTextBox <WindowsFormsHost x:Name="wfh"> <wf:RichTextBox x:Name="txtMain" HideSelection="False" ReadOnly="True" Multiline="True"/> </WindowsFormsHost> 我希望键盘焦点保持在TextBox,但仍然允许用户在WFH的RichTextBox中选择文本并
<WindowsFormsHost x:Name="wfh">
<wf:RichTextBox x:Name="txtMain" HideSelection="False" ReadOnly="True" Multiline="True"/>
</WindowsFormsHost>
我希望键盘焦点保持在TextBox
,但仍然允许用户在WFH的RichTextBox
中选择文本并滚动
我曾尝试在txtMain
中的MouseUp
上更改键盘焦点,但是无法滚动,并且无法使用Ctrl+C进行复制
有什么想法吗?你可能想看看这个。如果需要多焦点,则必须使用
聚焦镜
链接中的示例代码
static void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
IInputElement focusedElement = e.NewFocus;
for (DependencyObject d = focusedElement as DependencyObject;
d != null; d = VisualTreeHelper.GetParent(d)) {
if (FocusManager.GetIsFocusScope(d)) {
d.SetValue(FocusManager.FocusedElementProperty, focusedElement);
if (!(bool)d.GetValue(IsEnhancedFocusScopeProperty)) {
break;
}
}
}
}
我不确定这是否可能。通常,当您向下滚动或选择某个组件中的文本时,焦点应该放在该组件上。如果我可以问的话,你为什么要这么做?主要是为了更好的用户体验。由于这是一个聊天窗口,我希望用户能够随时准备键入。请注意,像Google Talk这样的程序具有此功能(键盘聚焦于文本框,但仍能在其他地方选择/复制文本)。@Arsham可以使用
FocusScope
@lll Cool!从没想过这是可能的。我将查看链接。:-)谢谢,看了样品!
static void OnGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
IInputElement focusedElement = e.NewFocus;
for (DependencyObject d = focusedElement as DependencyObject;
d != null; d = VisualTreeHelper.GetParent(d)) {
if (FocusManager.GetIsFocusScope(d)) {
d.SetValue(FocusManager.FocusedElementProperty, focusedElement);
if (!(bool)d.GetValue(IsEnhancedFocusScopeProperty)) {
break;
}
}
}
}