[英]WPF event handler in ItemContainerStyle
I have DataGrid 我有DataGrid
<DataGrid x:Name="EFDataGrid"
Visibility="Hidden"
AutoGenerateColumns="False"
ItemsSource="{Binding Path=ExchangeFile}"
SelectionMode="Single"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False">
<DataGrid.ItemContainerStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseRightButtonDown"
Handler="Row_MouseRightButtonDown" />
</Style>
</DataGrid.ItemContainerStyle>
<DataGrid.Columns>some columns</DataGrid.Columns>
</DataGrid>
and this handler in code behind 这个处理程序在代码后面
private void Row_MouseRightButtonDown(object sender, MouseEventArgs e)
{
some methods
}
but when I do right click on a row nothing going, event handler does not executes. 但是当我在一行上单击鼠标右键时,事件处理程序将不会执行。
What I do wrong? 我做错了什么?
Is there another way to handle this event(Right click on a row)? 还有另一种处理此事件的方法(右键单击行)?
void Row_MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
some methods
}
You worte MouseEventArgs e instead of MouseButtonEventArgs e . 您可以使用MouseEventArgs e代替MouseButtonEventArgs e 。 Is that a typo?
那是错字吗? If not, use MouseButtonEventArgs instead.
如果不是,请改用MouseButtonEventArgs。
MouseEventArgs is used for MouseLeave, MouseMove etc. MouseButtonEventArgs is used for MouseRightButtonDown, MouseRightButtonUp etc. MouseEventArgs用于MouseLeave,MouseMove等。MouseButtonEventArgs用于MouseRightButtonDown,MouseRightButtonUp等。
I think you need RoutedEventArgs
instead of MouseEventArgs
我认为您需要
RoutedEventArgs
而不是MouseEventArgs
private void Row_MouseRightButtonDown(object sender, RoutedEventArgs e)
{
//Some Method
}
and you should create the event on DataCell
instead of DataGridRow
并且您应该在
DataCell
而不是DataGridRow
上创建事件
<Style TargetType="EFDataGrid:DataCell">
<EventSetter Event="MouseRightButtonDown" Handler="Row_MouseRightButtonDown"/>
</Style>
You can also create an attached behavior for right clicking 您还可以为右键单击创建附加行为
You can try the following: 您可以尝试以下方法:
<DataGrid ItemsSource="{Binding ...}">
<DataGrid.Resources>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseRightButtonDown" Handler="Row_MouseRightButtonDown"/>
</Style>
</DataGrid.Resources>
</DataGrid>
Than you don't need the ItemContainerStyle 比您不需要ItemContainerStyle
use RoutedEventArgs as mentioned above! 如上所述使用RoutedEventArgs!
private void Row_MouseRightButtonDow(object sender, RoutedEventArgs e)
{
do stuff
}
Solved by using RowStyle
instead of ItemContainerStyle
: 通过使用
RowStyle
代替ItemContainerStyle
解决:
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<EventSetter Event="MouseRightButtonDown" Handler="Row_MouseRightButtonDown" />
</Style>
</DataGrid.RowStyle>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.