If you do ShowDialog() in the PreviewMouseDown event of TextBox, the cursor does not fit when you click TextBox.

Asked 2 years ago, Updated 2 years ago, 107 views

In WPF, if you do ShowDialog() in the PreviewMouseDown event of TextBox, the cursor will not fit when you click TextBox.
We also confirmed that the PreviewMouseUp event does not occur after ShowDialog() within the PreviewMouseDown event.TextBox appears to be down after PreviewMouseDown.

What I want to do is not to use TextBox.Focus() to hover the cursor, but to operate TextBox as usual after ShowDialog() in the PreviewMouseDown event.
In other words, as usual, is the following behavior:

If you know the solution, please take care of it.

MainWindow.xaml

<Grid>
    <TextBox Height="30" Width="200" PreviewMouseDown="TextBox_PreviewMouseDown"/>
</Grid>

MainWindow.xaml.cs

namespace Sample
{
    public partial class MainWindow—Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void TextBox_PreviewMouseDown (object sender, MouseButtonEventArgse)
        {
            new Dialog().ShowDialog();
        }
    }
}

Dialog.xaml

<Grid>
    <Button Click="Button_Click"/>
</Grid>

Dialog.xaml.cs

namespace Sample
{
    public partial class Dialog —Window
    {
        public Dialog()
        {
            InitializeComponent();
        }

        private void Button_Click (object sender, RoutedEventArgse)
        {
            DialogResult=true;
        }
    }
}

c# wpf

2022-09-30 14:36

1 Answers

I didn't write the code and check it, but
TextBox PreviewMouseDown is displaying a dialog, so
A dialog appears before PreviewMouseUp occurs.

When you close the dialog and return to the original form,
I don't think the mouse button is pressed, so
I think PreviewMouseUp does not occur.

The solution is not to use PreviewMouseDown events, but
PreviewMouseUp also handles the results from the dialog display.
*With PreviewMouseUp only, depending on the timing of the user's operation
An unintended dialog will appear, so
It should be combined with PreviewMouseDown.


2022-09-30 14:36

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.