0

I am writing a WPF application in C# according to the MVVM model. I have a MainWindow which consists of several views: MenuView, ContentView1, ContentView2, . . . and so on.

The MenuView, ContentView1, ContentView2 . . ., are all cousins (not bothers) in the XAML tree.

The MenuView consists of few buttons. Each button has a unique ID. The Content Views (all of them) should receive the menu ID of a selected (clicked) button. i.e. when a button in the MenuView is clicked I want to pass its ID to ContentsViews1, ContentsViews2 and so on.

I am not sure what is the best way to pass data between the views.

I thought to attach an event handler to all menu buttons which reside in the MenuView. The handler is implemented in MainWindow class (which is the root view). This handler will raise a RoutedEvent (with tunnel option) to all children views.

Is there a better way to do it? Is it the best way considering performance issue?

4

2 回答 2

0

您可以简单地MenuViewModel公开一个其他(感兴趣的)ViewModel 将订阅的事件。然后每个 View 可以绑定相应的事件并做它需要做的事情。

这就留下了一个问题,即每个感兴趣的 ViewModel 将如何检测到它的存在MenuViewModel以便它可以订阅其事件,但是如果没有更多关于您的应用程序和您选择的 MVVM 框架的知识,就无法真正回答这个问题。

于 2012-04-25T16:09:00.150 回答
0

这种方法让我想起了我久违的 java 经验,你传递了某种动作字符串!?给听众。

您将所有按钮绑定到一个处理程序?根据 ID,您想在子视图中执行操作吗?

如果是,您应该将所有按钮绑定到它自己的 ICommand 并在处理程序委托中执行操作。

于 2012-04-25T15:44:28.180 回答