如何使用 TreeView 连接 Silverlight 和 MVVM 中的视图?
我正在构建一个 Silverlight 应用程序,其中包含左侧列中的菜单选项 TreeView 和右侧列中的 ContentView。这个想法是 TreeView 的 SelectedItemChanged
事件将更改内容区域中的视图。
实现这一目标的“最纯粹的 MVVM”方式是什么?
我的想法是使用 TreeMenuView
和 TreeMenuViewModel
来管理菜单事件,但在那之后我有点迷失了。我可以使用 EventAggregator 将消息从 TreeMenuViewModel 发送到“ContentViewModel”,然后根据消息参数设置其当前的 ContentView,但这肯定会破坏 MVVM,从某种意义上说,ViewModel 应该不了解 View 等 UI 结构?
我在这里缺少一些简单的东西吗?
ViewModel 层如何驱动视图选择?
I am building a Silverlight app which comprises a TreeView of menu options in a lefthand column and a ContentView in a righthand column. The idea is that the SelectedItemChanged
event of the TreeView will change the view in the content area.
What is the 'purest MVVM' way of achieving this?
My idea is to have a TreeMenuView
and TreeMenuViewModel
for managing the menu events, but after that I'm a bit lost. I could use an EventAggregator to send a message from the TreeMenuViewModel
to a `ContentViewModel' that would then set its current ContentView based on the message args- but surely that breaks MVVM, in the sense that a ViewModel shouldn't know about UI constructs like a View?
Am I missing something simple here?
How does a ViewModel layer drive the View selection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将创建一个 ShellViewModel ,其中包含:
int SelectedPageIndex
ViewModelBase CurrentPage
,它返回AvailablePages[SelectedPageIndex]
您的
ShellView
可以是您想要的任何内容想。如果您想在TreeView
中显示AvailablePages
,请继续。只需记住将SelectedIndex
绑定到`SelectedPageIndex
在您的情况下,我将创建一个带有
TreeView
的DockPanel
左侧绑定到AvailablePages
,右侧绑定ContentControl
,其中ContentControl.Content
绑定到CurrentPage
Edit
这是然后
使用
DataTemplates
定义包含CurrentPage
的 ContentControl 的外观I would create a
ShellViewModel
which had:ObservableCollection<ViewModelBase> AvailablePages
int SelectedPageIndex
ViewModelBase CurrentPage
, which returnsAvailablePages[SelectedPageIndex]
Your
ShellView
can be anything you want. If you want to display yourAvailablePages
in aTreeView
, then go ahead. Just remember to bindSelectedIndex
to`SelectedPageIndex
In your case, I would create a
DockPanel
with aTreeView
on the Left bound toAvailablePages
, and aContentControl
on the right withContentControl.Content
bound toCurrentPage
Edit
Here's an example
Then use
DataTemplates
to define how the ContentControl containingCurrentPage
will look好的,我在 TreeMenuViewModel 中尝试一下
:
在
TreeMenuView
中:在 ContentViewModel 中:
在 ContentView 中:
并且您需要一个值转换器
Ok I give it a shot
in TreeMenuViewModel:
in
TreeMenuView
:<TreeView Context="{Binding TreeMenuViewModel}" Content="{Binding PropSelectedItem, Mode=OneWayToSource}"/>
in ContentViewModel:
in ContentView:
<ContentControl Context="{Binding TreeMenuViewModel}" Content="{Binding PropSelectedItem, Mode=OneWay}"/>
and you need a value convertor here