右键单击 Silverlight 4 应用程序中的列表框
- 作者: Black66506319
- 来源: 51数据库
- 2022-10-20
问题描述
我正在尝试实现我过去在 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:
- 右键单击列表框
- 将项目放在我点击突出显示的位置
- 我想要弹出一个上下文菜单(在上下文菜单中有我自己的项目)
从我目前的研究来看,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.
我的问题如下:
- 要完成 #2,我需要对列表框进行某种命中测试.我不知道该怎么做,我的 google-fu 也没有帮助.
- 一旦我确定了鼠标下的索引,我该如何实际选择项目?
- 是否有我可以使用的可重用上下文菜单组件?如果组件允许任意子菜单,则额外加分.
推荐答案
我一直在寻找同样的事情.我在 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:
为您的列表框创建一个 ItemTemplate
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 :)
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
