【问题标题】:Bind events to Item ViewModel将事件绑定到 Item ViewModel
【发布时间】:2014-05-15 09:51:10
【问题描述】:

我有一个 ListView/GridView 设置,我想在显示的项目上单击鼠标右键。有数据绑定方式吗?我见过像handling the super-elements eventand poking around to find its origin 这样的复杂解决方法,但是对于这样的基本请求,感觉非常臃肿。

我希望看到类似将事件绑定到项目的 ViewModel 的操作 - 有没有办法做到这一点?类似于this,但我无法完全理解如何调整它以适用于单个 ListView 项目(我什至不确定这是否可能,tbh)。

粗略的轮廓:

<ListView>
    <ListView.View>
        <GridView />
    </ListView.View>
    <ListView.Resources>
        <Style TargetType="{x:Type ListViewItem}">
        </Style>
    </ListView.Resources>
</ListView/>

【问题讨论】:

    标签: c# wpf mvvm


    【解决方案1】:

    有一种方法可以使用 Blend SDK 中的交互程序集。它将提供一个EventTrigger,它会在引发事件时执行命令。

    <!--
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    -->
    
    <Button Content="Click me">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <i:InvokeCommandAction Command="{Binding ClickCommand}" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
    

    编辑:

    您的问题的可能解决方案如下所示:

    查看:

    <ListView x:Name="listView">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseRightButtonUp">
                <i:InvokeCommandAction Command="{Binding RightClickOnItemCommand}"
                                       CommandParameter={Binding SelectedItem, ElementName=listView} />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </ListView>
    

    视图模型:

    public ICommand RightClickOnItemCommand { get; set; }
    
    public void RightClickOnItem(object item)
    {
    }
    

    【讨论】:

    • 这看起来确实是我想要的,但我该如何应用呢?这会通过风格起作用吗?如果您可以尝试将其合并到我添加到问题中的 sn-p 中,那就太好了。
    • 我为您的具体问题添加了一个可能的解决方案。
    • 这需要一些时间才能正确理解,但这正是我想要的;非常感谢,请接受!
    【解决方案2】:

    您可以尝试为列表视图项创建样式模板,并为其添加附加行为以处理鼠标单击。

    public static readonly DependencyProperty PreviewMouseLeftButtonDownCommandProperty =
            DependencyProperty.RegisterAttached("PreviewMouseLeftButtonDownCommand", typeof (ICommand),
                typeof (MouseBehaviour), new FrameworkPropertyMetadata(PreviewMouseLeftButtonDownCommandChanged));
    
        private static void PreviewMouseLeftButtonDownCommandChanged(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs args)
        {
            var element = (FrameworkElement) dependencyObject;
            element.PreviewMouseLeftButtonDown += Element_PreviewMouseLeftButtonDown;
        }
    
        private static void Element_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs args)
        {
            var element = (FrameworkElement) sender;
            ICommand command = GetPreviewMouseLeftButtonDownCommand(element);
            if (command != null)
            {
                command.Execute(args);
            }
        }
    
        public static void SetPreviewMouseLeftButtonDownCommand(UIElement element, ICommand value)
        {
            element.SetValue(PreviewMouseLeftButtonDownCommandProperty, value);
        }
    
        public static ICommand GetPreviewMouseLeftButtonDownCommand(UIElement element)
        {
            return (ICommand) element.GetValue(PreviewMouseLeftButtonDownCommandProperty);
        } 
    

    【讨论】:

      猜你喜欢
      • 2011-12-17
      • 2017-12-14
      • 1970-01-01
      • 2011-10-31
      • 2019-02-03
      • 1970-01-01
      • 2013-02-11
      • 2021-12-01
      • 1970-01-01
      相关资源
      最近更新 更多