【问题标题】:mvvm with prism: setting view from menu item带棱镜的 mvvm:从菜单项设置视图
【发布时间】:2013-08-18 15:19:39
【问题描述】:

我是 wpf 世界的新手。我在 shell 中有一个上下文菜单,如下所示:

              <ContextMenu>

                <MenuItem Header="Login" 
                          Command="{Binding WorkSpaceViewSetter}" CommandParameter="DemoApplication.View.LoginView">

                    <MenuItem.Icon>
                        <Image Height="16" Width="16" Stretch="Uniform" Source="/Images/login.png"/>
                    </MenuItem.Icon>

                </MenuItem>

                <MenuItem Header="Modules" ItemsSource="{Binding AppModules}">

                    <MenuItem.Icon>
                        <Image Source="/Images/modules.png"/>
                    </MenuItem.Icon>

                    <MenuItem.ItemContainerStyle>
                        <Style TargetType="MenuItem">
                            <Setter Property="Header" Value="{Binding ModuleName}"/>
                            <Setter Property="Command" Value="{Binding ElementName=win, Path=DataContext.WorkSpaceViewFromType}"/>  
                            <Setter Property="CommandParameter" Value="{Binding MainViewType}"/>                       
                        </Style>
                    </MenuItem.ItemContainerStyle>

                </MenuItem>

           </ContextMenu>

Modules menuitem 的 itemssource AppModules 中的每个元素都有一个名为 MainViewType 的属性,类型为 System.Type。我想在单击菜单项时更改区域的视图,并考虑在shellviewmodel 中使用单个ICommad 并将MainViewType 作为命令参数传递。但是,上面的代码不起作用。 我想知道为什么 Modules menuitem 会像预期的那样从 itemssource 填充。

我注意到Login menuitem 上的命令绑定也不起作用,即使它应该有,因为Modulesitemssource 属性已正确绑定。有人可以建议如何使它工作吗?

【问题讨论】:

  • ICommand 的 Execute 是什么样的?
  • 我正在使用 Microsoft.Practices.Prism.Commands 的 DelegateCommand

标签: wpf data-binding prism icommand


【解决方案1】:

上下文菜单与窗口的其余部分不在同一可视树上,因此在绑定中使用 ElementName 将不起作用。您需要改用PlacementTarget。在不知道您的视图模型的结构的情况下,很难给出明确的答案,但您的解决方案将类似于:

<MenuItem.ItemContainerStyle>
    <Style TargetType="MenuItem">
      <Setter Property="Header" Value="{Binding ModuleName}"/>
      <Setter Property="Command" Value="{Binding PlacementTarget.DataContext.WorkSpaceViewFromType}"/>  
      <Setter Property="CommandParameter" Value="{Binding MainViewType}"/>                       
   </Style>
</MenuItem.ItemContainerStyle>

【讨论】:

    猜你喜欢
    • 2011-08-11
    • 2011-02-15
    • 1970-01-01
    • 2017-03-26
    • 1970-01-01
    • 2013-12-27
    • 2012-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多