Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/9.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# 如何在RichTextBox中选择文本,同时在另一个文本框中保持键盘焦点_C#_Wpf_Winforms - Fatal编程技术网

C# 如何在RichTextBox中选择文本,同时在另一个文本框中保持键盘焦点

C# 如何在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中选择文本并

我正在WPF中开发一个聊天窗口,其中有一个Forms.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;
            }
        }
    }
}