确定哪些执行文本菜单菜单项时的ListViewItem被点击在ListView [英] Determining which ListViewItem was clicked on in a ListView when executing a ContextMenu MenuItem

查看:111
本文介绍了确定哪些执行文本菜单菜单项时的ListViewItem被点击在ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用上下文菜单在ListView运行一些code,需要从它起源于哪个项目数据。

I'm trying to use the context menu in a listview to run some code that requires data from which item it originated from.

我最初只是这样做:

XAML:

    <ListView x:Name="lvResources" ScrollViewer.VerticalScrollBarVisibility="Visible">
      <ListView.Resources>
        <ContextMenu x:Key="resourceContextMenu">
            <MenuItem Header="Get Metadata" Name="cmMetadata" Click="cmMetadata_Click" />
        </ContextMenu>
      </ListView.Resources>
      <ListView.ItemContainerStyle>
          <Style TargetType="{x:Type ListViewItem}">
              <Setter Property="ContextMenu" Value="{StaticResource resourceContextMenu}" />
          </Style>
      </ListView.ItemContainerStyle>
 ...

C#:

    private void cmMetadata_Click(object sender, RoutedEventArgs e)
    {
      // code that needs item data here
    }

但是我发现,始发ListView项是无法访问的方式。

But I found that the originating listview item was not accessible that way.

我读过关于如何解决这个问题,像拦截MouseDown事件和私有字段设置为被点击的ListViewItem的一些战术,但似乎有点哈克传不和我一起坐好围绕这样的数据。和WPF应该很容易吧? :)我读过这个<一个href=\"http://stackoverflow.com/questions/747872/wpf-displaying-a-context-menu-for-a-gridviews-items\">SO问题这 MSDN论坛的问题,但我仍然不知道如何真正做到这一点,因为无论这些条款似乎在我的情况下工作。有没有更好的方式来传递被点击,通过上下文菜单?

I've read some tactics about how to get around this, like intercepting the MouseDown event and setting a private field to the listviewitem that was clicked, but that doesn't sit well with me as it seems a bit hacky to pass data around that way. And WPF is supposed to be easy, right? :) I've read this SO question and this MSDN forum question, but I'm still not sure how to really do this, as neither of those articles seem to work in my case. Is there a better way to pass the item that was clicked on through to the context menu?

谢谢!

推荐答案

所以我决定尝试和执行命令的解决方案。我是pretty高兴它是如何工作的吧。

So I decided to try and implement a command solution. I'm pretty pleased with how it's working now.

首先,创建了我的命令:

First, created my command:

public static class CustomCommands
{
    public static RoutedCommand DisplayMetadata = new RoutedCommand();
}

在我的自定义ListView控件接下来,我添加了一个新的命令绑定的构造器:

Next in my custom listview control, I added a new command binding to the constructor:

public SortableListView()
{
    CommandBindings.Add(new CommandBinding(CustomCommands.DisplayMetadata, DisplayMetadataExecuted, DisplayMetadataCanExecute));
}

和还存在,增加了事件处理程序:

And also there, added the event handlers:

public void DisplayMetadataExecuted(object sender, ExecutedRoutedEventArgs e)
{
    var nbSelectedItem = (MyItem)e.Parameter;

    // do stuff with selected item
}

public void DisplayMetadataCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
    e.CanExecute = true;
    e.Handled = true;
}

我已经使用样式选择动态分配样式列表视图的项目,所以不是在XAML这样做的,我必须设置在codebehind绑定。你能做到这一点在XAML以及虽:

I was already using a style selector to dynamically assign styles to the listview items, so instead of doing this in the xaml, I have to set the binding in the codebehind. You could do it in the xaml as well though:

public override Style SelectStyle(object item, DependencyObject container)
{
    ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(container);
    MyItem selectedItem = (MyItem)item;
    Style s = new Style();

    var listMenuItems = new List<MenuItem>();
    var mi = new MenuItem();
    mi.Header= "Get Metadata";
    mi.Name= "cmMetadata";
    mi.Command = CustomCommands.DisplayMetadata;
    mi.CommandParameter = selectedItem;
    listMenuItems.Add(mi);

    ContextMenu cm = new ContextMenu();
    cm.ItemsSource = listMenuItems;

    // Global styles
    s.Setters.Add(new Setter(Control.ContextMenuProperty, cm));

    // other style selection code

    return s;
}

我喜欢这个解决方案不是试图设置鼠标点击一个字段,并尝试访问哪些被点击的方式更好的感觉。

I like the feel of this solution much better than attempting to set a field on mouse click and try to access what was clicked that way.

这篇关于确定哪些执行文本菜单菜单项时的ListViewItem被点击在ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆