右键单击Silverlight 4应用程序中的列表框 [英] Right-click on a Listbox in a Silverlight 4 app

查看:88
本文介绍了右键单击Silverlight 4应用程序中的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现以前在Winforms应用程序中理所当然的功能.我是Silverlight菜鸟,所以希望所有这些都是基本知识.

I am trying to implement what I used to take for granted in Winforms applications. I am a Silverlight noob, so hopefully all this is elementary.

我在Silverlight 4应用程序中有一个列表框.我想执行以下操作:

I have a listbox in a Silverlight 4 app. I'd like to do the following:

  1. 右键单击列表框
  2. 将项目放置在我点击突出显示的位置下方
  3. 我想弹出一个上下文菜单(上下文菜单中有我自己的项目)

到目前为止,根据我的研究,似乎在Silverlight中没有ContextMenu构造,相反,我们必须构建一个Grid/Canvas结构并将其附加到Popup对象,然后弹出该对象.

From my research so far, it appears that there is no ContextMenu construct in Silverlight, instead we have to build up a Grid/Canvas structure and attach it to a Popup object, which is what is then popped up.

我的问题如下:

  1. 要完成第二项,我需要对列表框进行某种命中测试.我不知道该怎么做,而我的google-fu也帮不上忙.
  2. 一旦确定了鼠标下的索引,我该如何实际选择该项目?
  3. 我可以在某个地方使用可重用的上下文菜单组件吗?如果组件允许任意子菜单,则可额外获得信用.

推荐答案

我一直在寻找相同的东西.我在CodePlex上检查了 Silverlight Control Toolkit 并进行了示例(这是非常方便的资源),这就是我发现是您所要求的解决方案:

I've been looking around for the same thing. I checked the Silverlight Control Toolkit at CodePlex and went through the samples (it's a very handy resource) and here's what I found to be the solution to what you asked:

  1. 为您的列表框创建一个ItemTemplate

  1. Create an ItemTemplate for your ListBox

在您要成为ItemTemplate的右键单击"部分中,设置System.Windows.Controls.Input.Toolkit名称空间中存在的附加属性ContextMenuService.ContextMenu

in the part that you want to be "right-clickable" of your ItemTemplate set the attached property ContextMenuService.ContextMenu that exists within the System.Windows.Controls.Input.Toolkit namespace

将MenuItem控件添加到ContextMenu并将Click属性设置为相应的click事件处理程序

add MenuItem controls to your ContextMenu and set the Click property to the corresponding click event handler

在事件处理程序中,从发送方获取DataContext(您可以使用该上下文在ListBox中找到相应的元素)

in the event handler, get the DataContext from the sender (you can use that to find the corresponding element in the ListBox)

要使该元素处于选中状态,只需在列表框中为其设置SelectedItem属性

to make that element Selected, just set the SelectedItem property in the list box to it

向事件处理程序添加任何自定义逻辑

Add any custom logic to the event handler

示例页面中有一个示例,只需从导航窗格中转到"Input-> ContextMenu"即可.

There's an example in the samples page, just go to "Input->ContextMenu" from the navigation pane.

如果您想简洁明了,这是一个简化的示例:

If you want something concise, Here's a simplified example:

<ListBox ItemsSource="{StaticResource People}"
             Name="myListBox">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Name}">
                    <controlsInputToolkit:ContextMenuService.ContextMenu>
                        <controlsInputToolkit:ContextMenu>
                            <controlsInputToolkit:MenuItem Header="Show in MessageBox"
                                                           Click="show_Click" />
                        </controlsInputToolkit:ContextMenu>
                    </controlsInputToolkit:ContextMenuService.ContextMenu>
                </TextBlock>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

具有:

xmlns:controlsInputToolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input.Toolkit"

获取代码:

private void show_Click(object sender, RoutedEventArgs e)
    {
        var person = ((MenuItem)sender).DataContext as Person;
        if (null == person) return;
        MessageBox.Show("My Name is: " + person.Name);
        myListBox.SelectedItem = person;
    }

我希望这会有所帮助:)

I hope this helps :)

这篇关于右键单击Silverlight 4应用程序中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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