首页 WinForm/WPF WPF之datagrid里面的按钮不能触发点击事件

WPF之datagrid里面的按钮不能触发点击事件

作者:胡同里的砖头 围观群众:199 更新于:2023-09-21

在WPF里面我们通常会放室一个按钮点击编辑或删除,但是在按钮的InvokeCommandAction触发器里面写MouseLeftButtonUP 并不能触发,如下图代码

<TextBlock Text="移除" Margin="0,0,0,0"  HorizontalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="DelFunCommand" CommandParameter="{Binding SelectedRow}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
上列代码点击始终无效,DataContext它试图寻找的是DataContext对于Template根据您的结构,这可能会有所不同。

改成以下
<TextBlock Text="移除" Margin="0,0,0,0"  HorizontalAlignment="Center">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonUp">
<i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.DelFunCommand}" CommandParameter="{Binding SelectedRow}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
1.主要是把EventName里面鼠标事件改成Button 上的隧道事件 PreviewMouseLeftButtonUp,而不是 MouseLeftButtonUp

2.然后把Command里面的方法通过DataContext来定义方法名称:Command="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}, Path=DataContext.DelFunCommand}"

  • 本文标题: WPF之datagrid里面的按钮不能触发点击事件
  • 文章分类:【WinForm/WPF】
  • 非特殊说明,本文版权归【胡同里的砖头】个人博客 所有,转载请注明出处.
留言评论
站点声明:
1、本站【胡同里的砖头】个人博客,借鉴网上一些博客模板,取其各优点模块自行拼装开发,本博客开发纯属个人爱好。
2、所有笔记提供给广大用户交流使用,可转载,可复制,纯个人开发所遇问题锦集记录使用
Copyright © huzlblog.com All Rights Reserved. 备案号:苏ICP备2021056683号-8