Touch Error in WPF?

I made the following application (as a test)

XAML:

<Window x:Class="GUITest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid Background="Transparent"> <TextBlock Text="openDialog" Background="Red" HorizontalAlignment="Center" VerticalAlignment="Top" MouseDown="TextBlock_MouseDown" /> </Grid> </Window> 

WITH#:

 public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void TextBlock_MouseDown(object sender, MouseButtonEventArgs e) { OpenFileDialog dlg = new OpenFileDialog(); Nullable<bool> result = dlg.ShowDialog(); if (result == true) { Console.Out.WriteLine(dlg.FileName); } } } 

I will catch the mouseDown event because it catches mouse events and mouse buttons. The code has the expected behavior for mouse clicks. Touching causes some problems.

If I touch TextBlock, it will open a dialog box as specified. after closing it, any touch of the window opens a dialog box, even if the touch was not in the TextBlock.

This is mistake? Can I get around this?

EDIT: I posted a workaround, the actual fix will still be useful

+6
source share
1 answer

For other people facing the same problem. This is not a solution to the problem, but a workaround. I used a button and remade it to look like a TextBlock

XAML:

 <Grid Background="Transparent"> <Button HorizontalAlignment="Left" VerticalAlignment="Top" Click="Button_Click" Content="openDialog"> <Button.Template> <ControlTemplate TargetType="Button"> <ContentPresenter /> </ControlTemplate> </Button.Template> </Button> </Grid> 

The code for Button_Click is similar to TextBlock_MouseDown

0
source

Source: https://habr.com/ru/post/977425/


All Articles