[英]Can't access the events inside the GridViewColumn's Content
When I add an event to the ListViewItem
, 当我将事件添加到
ListViewItem
,
<Style TargetType="{x:Type ListViewItem}">
<EventSetter Event="PreviewMouseDown" Handler="listViewItem_MouseDown" />
</Style>
Then add another event in the content inside the GridViewColumn
, 然后在
GridViewColumn
内部的内容中添加另一个事件,
<GridViewColumn Header="Action">
<GridViewColumn.CellTemplate>
<DataTemplate>
<Image x:Name="imgEdit"
VerticalAlignment="Center"
HorizontalAlignment="Left"
Width="40"
Height="40"
Tag="{Binding ProductBarcode}"
Cursor="Hand"
MouseDown="img_MouseDown">
<Image.Style>
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="/Resources/edit_button.png" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="/Resources/edit_button_hovered.png" />
</Trigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
I can't fire the event in the Image
control. 我无法在
Image
控件中触发该事件。 The one that is firing is the event in the ListViewItem
. 引发事件的是
ListViewItem
的事件。 How to access the event in the Image
control? 如何在
Image
控件中访问事件?
Here are the events: 这是事件:
private void img_MouseDown(object sender, MouseButtonEventArgs e)
{
switch ((sender as Image).Name)
{
case "imgEdit":
MessageBox.Show("EDIT");
break;
default:
break;
}
}
private void listViewItem_MouseDown(object sender, RoutedEventArgs e)
{
MessageBox.Show("ROW CLICKED");
}
The PreviewMouseDown
event of the ListViewItem
will always be raised before the MouseDown
event of the Image. ListViewItem
的PreviewMouseDown
事件将始终在Image的MouseDown
事件之前引发。 This is how routed events work: https://msdn.microsoft.com/en-us/library/ms742806(v=vs.110).aspx . 路由事件的工作方式如下: https : //msdn.microsoft.com/zh-cn/library/ms742806(v=vs.110).aspx 。
PreviewMouseDown
is a tunneling event and MouseDown
is a bubbling event. PreviewMouseDown
是一个隧道事件,而MouseDown
是一个冒泡事件。
If you don't want to handle the PreviewMouseDown
event when the Image
is clicked you could check the type of the OriginalSource
of the RoutedEventArgs
and return immediately from the event handler if it is Image
: 如果您不想在单击
Image
时处理PreviewMouseDown
事件,则可以检查RoutedEventArgs
的OriginalSource
的类型,如果它是Image
则从事件处理程序中立即返回:
private void listViewItem_MouseDown(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is Image)
return; // do nothing
MessageBox.Show("ROW CLICKED");
}
Try: 尝试:
private void listViewItem_MouseDown(object sender, MouseButtonEventArgs e)
{
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
MessageBox.Show("ROW CLICKED");
}));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.