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

查看:17
本文介绍了右键单击 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. 要完成 #2,我需要对列表框进行某种命中测试.我不知道该怎么做,我的 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 中存在的附加属性 ContextMenuService.ContextMenu.Toolkit 命名空间

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 属性设置为相应的点击事件处理程序

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天全站免登陆