如何使用 mvvm 和 WPF 管理工具栏

发布于 2024-08-27 16:50:31 字数 232 浏览 4 评论 0原文

我正在寻找一种在 WPF 中使用 mvvm 管理工具栏(和菜单)的流畅方法。

考虑具有选项卡式工作区和异构内容的 UI(如 Visual Studio)。那里的工具栏应该隐藏或可见,具体取决于活动选项卡。您将如何设计工具栏的视图视图模型?

我会使用工具栏视图模型的集合并将 ToolbarTray 绑定到它,但据我所知这是不可能的。

如有任何建议,我们将不胜感激。 欢迎提供示例、最佳实践论文等的链接。

I'm looking for a smooth method of managing toolbars (and menus) with mvvm in WPF.

Consider an UI with tabbed workspaces and heterogenous content (like Visual Studio). There the toolbars should be hidden or visible depending on the active tab. How would you design the view viewmodel for the toolbars?

I'd use a collection of toolbar-viewmodels and bind the ToolbarTray to it, but afaik that's not possible.

Any recommendations are apreciated.
Links to samples, best practice papers, etc. are welcome.

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

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

发布评论

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

评论(1

贱贱哒 2024-09-03 16:50:31

我最近在我的一个应用程序中遇到了同样的问题。我使用功能区作为主工具栏,并且根据选择的模块,必须隐藏或显示功能区上的按钮。我想出了两种不同的解决方案......希望其中一种适合您。

第一个解决方案是在应用程序的外壳中创建一个区域(可以是网格/画布/其他),并根据用户单击的选项卡将所需的工具栏动态加载到该区域中。换句话说,如果您有 5 个不同的选项卡需要它们自己的工具栏,请创建 5 个包含工具栏的 UserControl,并在用户单击选项卡时将正确的选项卡加载到区域中。接下来,您需要响应用户单击工具栏上的按钮时的事件。在我的解决方案中,我使用了 Mediator 方法允许 ViewModel 相互通信。它有效,但我不知道我是否会再次这样做...... CAL 可能是更好的方法。

另一种解决方案是创建一个包含所有必需按钮的工具栏,并将它们的可见性绑定到 ViewModel。根据用户的选择,将控件的可见性设置为您的要求。希望这些解决方案之一适合您。

顺便问一下...您来自德国的哪个地区?我认识一个姓斯托尔的人,所以我不确定这是否是一个常见的名字......

I had this same issue in one of my applications recently. I was using a Ribbon as my main toolbar, and based on what module was selected the buttons on the ribbon had to be hidden or shown. I came up with two different solutions... hopefully one will work for you.

The first solution would be to create a region in the shell of your application (it can be a Grid/Canvas/whatever) and dynamically load the desired toolbar into this region based on what tab the user clicks on. In other words, if you have 5 different tabs that require their own toolbar, create 5 UserControls that contain a toolbar and load the correct one into the region when the user clicks on the tab. Next you'll want to respond to the event of when the user clicks on a button on the toolbar. In my solution I used the Mediator approach to allow ViewModels to communicate with each other. It works, but I don't know that I would do it again that way... CAL is probably the better approach.

The other solution would be to create one toolbar with all of the required buttons and bind their visibility to the ViewModel. Based on the user's selection, set the visibility of the controls to whatever your requirements may be. Hopefully one of these solutions works for you.

by the way... what part of Germany are you from? I know a man with the last name of Stoll, so I wasn't sure if it was a common name or not...

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