Wpf 带prism的mvvm:从菜单项设置视图
我是wpf世界的新手。我在shell中有一个上下文菜单,如下所示:Wpf 带prism的mvvm:从菜单项设置视图,wpf,data-binding,prism,icommand,Wpf,Data Binding,Prism,Icommand,我是wpf世界的新手。我在shell中有一个上下文菜单,如下所示: <ContextMenu> <MenuItem Header="Login" Command="{Binding WorkSpaceViewSetter}" CommandParameter="DemoApplication.View.LoginView">
<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菜单项的itemssourceAppModules
中的每个元素都有一个名为MainViewType
的属性,其类型为System.type
。我想在单击菜单项时更改区域视图,我想在shellviewmodel
中使用单个ICommad
,并将MainViewType
作为命令参数传递。但是,上述代码不起作用。
我想知道为什么Modules
menuitem会像预期的那样从itemssource
中填充
我注意到,
Login
menuitem上的命令绑定也不起作用,即使它应该起作用,因为Modules
的itemssource
属性得到了正确的绑定。有人可以建议如何使用它吗?上下文菜单与窗口的其余部分不在同一个可视树上,因此在绑定中使用ElementName将不起作用。您需要改用PlacementTarget
。在不知道viewmodels是如何构造的情况下,很难给出明确的答案,但您的解决方案将类似于:
<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>
ICommand的执行是什么样子的?我正在使用Microsoft.Practices.Prism.Commands中的DelegateCommand。