【问题标题】:ListViewItem EventTrigger not firing WPFListViewItem EventTrigger 未触发 WPF
【发布时间】:2020-08-12 19:00:54
【问题描述】:

我有在 xaml ListViewItems 中定义的 ListView。我正在尝试通过 Microsoft.Xaml.Behaviors.Wpf 将命令绑定到 PreviewMouseLeftButtonDown 单击事件上的 LisdtViewItem,但它不起作用。

Xaml 代码:

                    <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>

ViewModelCode:


    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;
        }
    }

另外,我尝试在图像和边框内添加事件触发器,但这也无济于事......

【问题讨论】:

  • 在您的问题中您写“左鼠标单击”,但您的触发器在MouseDoubleClick 上触发。所以双击它应该可以工作。
  • @thatguy 我在有问题的 PreviewMouseLeftButtonDown 上进行了更改。谢谢你。我也尝试了 MouseDoubleClick 事件,但它没有用..
  • EventTriggers 不应该使用“RoutedEvents”吗?从来没有通过“EvenName”做到这一点。
  • @TomášBuchta 我在 xaml 中看不到 RoutedEvent 属性。也许这是因为我正在使用 Microsoft.Xaml.Behaviors EventTrigger...
  • 如果您需要一个即时有效的解决方案,这应该可以解决问题,而无需触发器或 Microsoft.Xaml.Behaviors : 和内部 ...如果这不起作用,则绑定/数据上下文也可能存在问题。无法提供更多帮助,抱歉。

标签: c# wpf


【解决方案1】:

问题是我使用 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();
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    相关资源
    最近更新 更多