我做了以下申请(作为测试)
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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我捕获了mouseDown事件,因为它捕获鼠标按钮事件和触发事件.代码具有鼠标单击的预期行为.触摸给我带来了一些麻烦.
如果我触摸TextBlock,它会打开一个询问的对话窗口.关闭后,即使触摸不在TextBlock上,窗口上的任何触摸都会打开对话框窗口.
这是一个错误吗?我可以解决这个问题吗?
编辑:我发布了一个解决方法,一个实际的修复仍然是有用的
我正在使用 Datagridview,通过设置 DataPropertyNames 部分由数据源填充。部分是我在代码中填写的。我有一个 DataGridViewComboBoxColumn,当我单击它旁边的按钮时,应填充其中的单元格。单击按钮填充 DataGridViewComboBoxCell 的项目,但如果我单击 ComboBox,则无法打开它来选择值。
(在函数末尾,我将列值更改为 x 作为测试,这工作正常......)
代码:
//add the combobox column
DataGridViewComboBoxColumn cellcol = new DataGridViewComboBoxColumn();
cellcol.Name = "Cell";
cellcol.ReadOnly = false;
dataGridView1.Columns.Add(cellcol);
//method that gets called when the button is clicked
private void getAllCells(List<ComponentRow> componentRows, int i)
{
//show all cells
CellFilter cf = new CellFilter();
cf.customerNr = libraryNr;
Cell[] cells = Service.Instance.Client.queryCells(cf, new QueryParam());
foreach (Cell c in cells)
{
((DataGridViewComboBoxCell)dataGridView1["Cell", i]).Items.Add(cells[0].cell_name);
}
cellsShownForComponents[i] = ShowedCells.ALL;
((DataGridViewTextBoxCell)dataGridView1["variants", i]).Value = "x";
}
Run Code Online (Sandbox Code Playgroud)