Is there a way to only focus a Textbox when it's double clicked?
Normally it gets focused when it's clicked once.
I want to be able to DragMove();
the Window if the Textbox is clicked and if it's double clicked I want to be able to write into it.
I've already tried this solution: TextBox readonly "on/off" between "double click and lost focus events" in wpf But it still focuses the Textbox.
I want to do the same thing with a Richtextbox I hope one solution works for both.
Thank you in advance :D
CodePudding user response:
Try it:
<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;
}
}
CodePudding user response:
I figured it out! I just had to tweak @Ohad Cohens Code a bit!
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;
}
}
The DragMove();
Can simply be achived using
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}