我做了以下申请(作为测试)
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>
C#:
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);
}
}
}
我捕获了mouseDown事件,因为它同时捕获了鼠标按下事件和触摸事件。该代码具有鼠标单击的预期行为。触摸给我带来了一些麻烦。
如果我触摸TextBlock,则会按要求打开一个对话框。关闭它后,即使触摸不在TextBlock上,窗口上的任何触摸也会打开对话框窗口。
这是一个错误吗?我可以解决这个问题吗?
编辑:我发布了一种解决方法,实际的修复仍将是有用的
最佳答案
对于遇到相同问题的其他人。这不是解决问题的方法,但是是一种解决方法。
我使用了一个按钮并将其重新设置为看起来像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>
Button_Click的代码与TextBlock_MouseDown相同