WPF MVVM:如何创建和使用显示上下文菜单?

发布于 2024-09-10 17:45:47 字数 158 浏览 0 评论 0原文

对于我的 WPF 应用程序,我使用 MVVM,现在我想在用户右键单击某些内容时显示生成的上下文菜单。
将右键单击路由到某些操作很容易,但如何显示上下文菜单,哪些项目是由 ViewModel 生成的?

我什至不知道从哪里开始显示上下文菜单,因为我无法直接访问 MVVM 中的视图。

For my WPF application I'm using MVVM, and now I want to show a generated context menu when the user right-clicks on something.
Routing the right-click to some action was easy, but how do I show a contextmenu which items are generated by the ViewModel?

I don't even have an idea where to start to display a context menu, since I do not have direct access to the view in MVVM.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

谁人与我共长歌 2024-09-17 17:45:47

对于回复延迟表示歉意,必须进行一些实验才能使其发挥作用。尝试一下以下代码。我只是设置了自己的垃圾数据源,这样我就可以显示某种数据。仅当我右键单击第一个列标题而不显示其他位置时,它才会显示...我认为这就是您想要的,对吧?让我知道你的进展如何……任何问题都会继续思考。

   <Grid>
    <ListView Margin="8,8,33,12"  ItemsSource="{Binding Source={StaticResource Stuff}, Path=MyCollection}">
        <ListView.View>
        <GridView>
                <GridViewColumn Width="100" DisplayMemberBinding="{Binding}">
                    <GridViewColumnHeader>ProductName
                        <GridViewColumnHeader.ContextMenu>
                            <ContextMenu Name="MyMenu">
                                <MenuItem Header="Sort by..."/>
                                <MenuItem Header="Follow link..."/>
                            </ContextMenu>
                        </GridViewColumnHeader.ContextMenu>
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn Width="100" Header="Product Name" DisplayMemberBinding="{Binding Path=Length}"/>
        </GridView>
        </ListView.View>
    </ListView>
</Grid>

Apologies for the delay in replying, had to have a bit of an experiment to get it to work. Give the following code a go. I just set up my own rubbish data source just so I could display some sort of data. It only displays if I right click over the first column heading and no where else... which I think is what you want, right? Let me know how you get on...any probs will continue to have a think.

   <Grid>
    <ListView Margin="8,8,33,12"  ItemsSource="{Binding Source={StaticResource Stuff}, Path=MyCollection}">
        <ListView.View>
        <GridView>
                <GridViewColumn Width="100" DisplayMemberBinding="{Binding}">
                    <GridViewColumnHeader>ProductName
                        <GridViewColumnHeader.ContextMenu>
                            <ContextMenu Name="MyMenu">
                                <MenuItem Header="Sort by..."/>
                                <MenuItem Header="Follow link..."/>
                            </ContextMenu>
                        </GridViewColumnHeader.ContextMenu>
                    </GridViewColumnHeader>
                </GridViewColumn>
                <GridViewColumn Width="100" Header="Product Name" DisplayMemberBinding="{Binding Path=Length}"/>
        </GridView>
        </ListView.View>
    </ListView>
</Grid>
温柔一刀 2024-09-17 17:45:47

例如,如果您想在可能显示销售的 DataGrid 上显示 ContextMenu,您可以这样做:

            <y:DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Name="cmNewSales" Foreground="Black" Command={Binding Path=MyCommand}/>


            </ContextMenu>
        </y:DataGrid.ContextMenu>

其中 MyCommand 是 ViewModel 公开的 Command 属性,或者在 ViewModel 中创建命令的 ObservableCollection,这些命令公开并绑定到 ContextMenu 的 ItemSource 属性中。

希望有帮助

If, for example, you wanted to show a ContextMenu over a DataGrid that maybe showed sales you could do this:

            <y:DataGrid.ContextMenu>
            <ContextMenu>
                <MenuItem Name="cmNewSales" Foreground="Black" Command={Binding Path=MyCommand}/>


            </ContextMenu>
        </y:DataGrid.ContextMenu>

where the MyCommand is a Command property exposed by the ViewModel, or create an ObservableCollection of commands in the ViewModel, which are exposed and bound to in the ItemSource property of the ContextMenu.

Hope that helps

心作怪 2024-09-17 17:45:47

我立即想到的答案是在虚拟机中拥有一个命令列表(对应于上下文菜单中的项目)。将 ContextMenu 的 ItemSource 绑定到 VM.ListOfCommands。根据口味使用样式。

这是同一行的一个例子......
http://www.julmar.com/blog/mark/ 2009/04/21/使用MVVMWithMenusInWPF.aspx

The off-the-top-of-my-head answer would be to have a list of commands (corresponding to the items in your context menu) in the VM. Bind the ContextMenu's ItemSource to the VM.ListOfCommands. Use Styles as per taste.

Here's an example on the same lines...
http://www.julmar.com/blog/mark/2009/04/21/UsingMVVMWithMenusInWPF.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文