c# - ListViewItem EventTrigger 未触发 WPF
问题描述
我有在 xaml ListViewItems 中定义的 ListView。我正在尝试通过 Microsoft.Xaml.Behaviors.Wpf 将命令绑定到 PreviewMouseLeftButtonDown 单击事件上的 LisdtViewItem,但它不起作用。
Xml代码:
<ListView x:Name="SideMenu"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
BorderThickness="0 0 1 0"
ItemContainerStyle="{StaticResource MenuItem}">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" VerticalAlignment="Stretch" ToolTip="{Binding ToolTip}">
<Image Source="{Binding ImageAddress}" Style="{StaticResource MenuIcon}"/>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
<ListViewItem IsSelected="True">
<Image Source="../Icons/TestPlan.png" Style="{StaticResource MenuIcon}"/>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding TestingCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListViewItem>
视图模型代码:
public sealed class SideMenuControlViewModel
{
public ICommand GetOddsCommand { get; set; }
public SideMenuControlViewModel()
{
GetOddsCommand = new RelayCommand(o => GetOdds());
}
public ICommand TestingCommand
{
get => new RelayCommand((s) => Test());
}
private void GetOdds()
{
}
private void Test()
{
int a = 5;
int b = a + a;
}
}
另外,我尝试在图像和边框内添加事件触发器,但这也无济于事......
解决方案
问题是我使用 ICommand 不是来自 System.Windows.Input 只是从教程中复制它......我只是改变了我的 RelayCommand 类继承和命令绑定开始照常工作
public sealed class RelayCommand : ICommand
{
private readonly Action _action;
public event EventHandler CanExecuteChanged = (sender, e) => { };
public RelayCommand(Action action)
{
_action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_action();
}
}
推荐阅读
- python - Python 无法识别有效的 RegEx 条目
- c# - WPF 中的弹出窗口无法打开
- sql - 如何在 Oracle SQL 中按顺序匹配当前和前几个月的日期?(例如 1-jun as monday 与 4th jun monday 匹配)
- r - 在 RStudio 中使用 quit() 而不提示保存对代码的更改
- vue.js - this.$vuetify.goTo(0) 在 vuetify 的 v-navigation-drawer 中不起作用
- java - 在 GridView 中添加多个文本视图
- r - 如何从 csv 文件创建一个数据框,其文本由管道 I 分隔?
- python - 我的“if”函数不适用于字符串?
- python-3.x - 在延迟函数中创建变量的效率
- java - 如何在处理来自 Arduino 的数据时创建一个实时更新的表?