2013-08-16 5 views
1

wpf world가 새로 생겼습니다. 메뉴 항목 유형 System.TypeMainViewType이라는 속성을 가지고있는 모듈의 itemssource AppModules에서프리즘이있는 mvvm : 메뉴 항목에서보기 설정

   <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> 

각 요소 : 나는 다음과 같이 셸에서 상황에 맞는 메뉴가 있습니다. menuitem이 클릭되었을 때 에서 하나의 ICommad을 사용하고 MainViewType을 명령 매개 변수로 사용하여 생각하고있는 지역의보기를 변경하고 싶습니다. 그러나 위 코드는 작동하지 않습니다. Modules menuitem이 itemssource에서 예상대로 채워진 이유가 궁금합니다.

속성이 Modules에 제대로 묶여 있으므로 Login menuitem의 명령 바인딩이 작동하지 않는 것으로 나타났습니다. 아무도 그것을 작동하게하는 방법을 제안 할 수 있습니까?

+0

ICommand의 실행 모습은 어떻습니까? –

+0

Microsoft.Practices.Prism.Commands의 DelegateCommand 을 사용하고 있습니다. –

답변

0

컨텍스트 메뉴는 나머지 창의 것과 동일한 시각적 트리에 있지 않으므로 바인딩에서 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>