仅在双击时聚焦文本框

Only focus a Textbox when it's double clicked

有没有办法只在双击文本框时聚焦它? 通常它会在单击一次时获得焦点。 如果单击文本框,我希望能够 DragMove(); Window,如果双击它,我希望能够写入它。

我已经尝试过这个解决方案:TextBox readonly "on/off" between "double click and lost focus events" in wpf 但它仍然聚焦文本框。

我想用 Richtextbox 做同样的事情我希望一个解决方案适用于两者。

提前谢谢你:D

试一试:

<TextBox MouseDoubleClick="TextBox_MouseDoubleClick" LostFocus="TextBox_LostFocus"/>
private void TextBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    if (sender is TextBox tb)
    {
        tb.IsReadOnly = false;
        tb.Focus();
    }
}

private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
    if (sender is TextBox tb)
    {
        tb.IsReadOnly = true;
    }
}

我想通了! 我只需要稍微调整一下@Ohad Cohens 代码!

XAML

<TextBox MouseDoubleClick="TB_MouseDoubleClick" LostFocus="TB_LostFocus" Focusable="False"/>

C#

        private void TB_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (sender is TextBox tb)
            {
                tb.Focusable = true;
                tb.Focus();
            }
        }

        private void TB_LostFocus(object sender, RoutedEventArgs e)
        {
            if (sender is TextBox tb)
            {
                tb.Focusable = false;
            }
        }

DragMove();

可以简单地使用

实现
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
    DragMove();
}