如何使用 TreeView 连接 Silverlight 和 MVVM 中的视图?

发布于 2024-12-02 04:27:42 字数 465 浏览 0 评论 0原文

我正在构建一个 Silverlight 应用程序,其中包含左侧列中的菜单选项 TreeView 和右侧列中的 ContentView。这个想法是 TreeView 的 SelectedItemChanged 事件将更改内容区域中的视图。

实现这一目标的“最纯粹的 MVVM”方式是什么?

我的想法是使用 TreeMenuViewTreeMenuViewModel 来管理菜单事件,但在那之后我有点迷失了。我可以使用 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 技术交流群。

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

发布评论

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

评论(2

世界如花海般美丽 2024-12-09 04:27:42

我将创建一个 ShellViewModel ,其中包含:

  • ObservableCollectionAvailablePages
  • int SelectedPageIndex
  • ViewModelBase CurrentPage,它返回 AvailablePages[SelectedPageIndex]

您的 ShellView 可以是您想要的任何内容想。如果您想在 TreeView 中显示 AvailablePages,请继续。只需记住将 SelectedIndex 绑定到 `SelectedPageIndex

在您的情况下,我将创建一个带有 TreeViewDockPanel左侧绑定到 AvailablePages,右侧绑定 ContentControl,其中 ContentControl.Content 绑定到 CurrentPage

Edit

这是然后

<DockPanel>
    <TreeView DockPanel.Dock="Right"
              ItemsSource="{Binding AvailablePages}"
              SelectedItem="{Binding SelectedPageIndex}">
    ...
    </TreeView>

    <ContentControl Content="{Binding CurrentPage}" />
</DockPanel>

使用 DataTemplates 定义包含 CurrentPage 的 ContentControl 的外观

<Window.Resources>
    <DataTemplate DataType="{x:Type local:HomePageViewModel}" />
         <local:HomePageView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:CustomerViewModel}" />
         <local:CustomerView />
    </DataTemplate>
</Window.Resources>

I would create a ShellViewModel which had:

  • ObservableCollection<ViewModelBase> AvailablePages
  • int SelectedPageIndex
  • ViewModelBase CurrentPage, which returns AvailablePages[SelectedPageIndex]

Your ShellView can be anything you want. If you want to display your AvailablePages in a TreeView, then go ahead. Just remember to bind SelectedIndex to `SelectedPageIndex

In your case, I would create a DockPanel with a TreeView on the Left bound to AvailablePages, and a ContentControl on the right with ContentControl.Content bound to CurrentPage

Edit

Here's an example

<DockPanel>
    <TreeView DockPanel.Dock="Right"
              ItemsSource="{Binding AvailablePages}"
              SelectedItem="{Binding SelectedPageIndex}">
    ...
    </TreeView>

    <ContentControl Content="{Binding CurrentPage}" />
</DockPanel>

Then use DataTemplates to define how the ContentControl containing CurrentPage will look

<Window.Resources>
    <DataTemplate DataType="{x:Type local:HomePageViewModel}" />
         <local:HomePageView />
    </DataTemplate>
    <DataTemplate DataType="{x:Type local:CustomerViewModel}" />
         <local:CustomerView />
    </DataTemplate>
</Window.Resources>
别念他 2024-12-09 04:27:42

好的,我在 TreeMenuViewModel 中尝试一下

public string PropSelectedItem
{
get;
set;
}

TreeMenuView 中:

在 ContentViewModel 中:

public ViewModelBase PropSelectedItem
{
get
 {
switch(TreeMenuViewModelStatic.PropSelectedItem)
{
  case "Booo": return typeof(View1);
  case "Foo": return typeof(View2);
}


  }
private set;
}

在 ContentView 中:

并且您需要一个值转换器

Ok I give it a shot

in TreeMenuViewModel:

public string PropSelectedItem
{
get;
set;
}

in TreeMenuView:

<TreeView Context="{Binding TreeMenuViewModel}" Content="{Binding PropSelectedItem, Mode=OneWayToSource}"/>

in ContentViewModel:

public ViewModelBase PropSelectedItem
{
get
 {
switch(TreeMenuViewModelStatic.PropSelectedItem)
{
  case "Booo": return typeof(View1);
  case "Foo": return typeof(View2);
}


  }
private set;
}

in ContentView:

<ContentControl Context="{Binding TreeMenuViewModel}" Content="{Binding PropSelectedItem, Mode=OneWay}"/>

and you need a value convertor here

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