2009-05-21 6 views
3

wpf 응용 프로그램에서 mvvm을 사용하고 있습니다. ContextMenu를 listview 안에 넣었습니다. listviewitem을 마우스 오른쪽 버튼으로 클릭하면 연락처 목록을 표시 할 컨텍스트 메뉴가 필요합니다.WPF ContextMenu

다음 내용은 내용이없는 컨텍스트 메뉴를 제공합니다. 아무도 내가 뭘 잘못하고 있다고 말할 수 있습니까?

<ListView Grid.Row="3" 
      ItemsSource="{Binding Path=Phones}" 
      SelectedItem="{Binding Phones.SelectedItem}" 
      Height="100"> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="{x:Type ListViewItem}"> 
      <Setter Property="ContextMenu" Value="{StaticResource ContactMenu}"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
    <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Phone" DisplayMemberBinding="{Binding Path=PhoneNumber, StringFormat=(000) 000-0000}"/> 
      <GridViewColumn Header="Type" DisplayMemberBinding="{Binding Path=PhoneType.Type}"/> 
      <GridViewColumn Header="Contacts" DisplayMemberBinding="{Binding Path=Contacts.Count}"/> 
      <GridViewColumn Header="Notes" DisplayMemberBinding="{Binding Path= Notes.Count}"/> 
      <GridViewColumn Header="Priority" DisplayMemberBinding="{Binding Path=Priority}"/> 
     </GridView> 
    </ListView.View> 
</ListView> 


<UserControl.Resources> 
    <Style TargetType="{x:Type ListViewItem}"> 
     <Setter Property="Background" Value="{Binding SourceType, Converter={StaticResource SourceGroupConverter}}"/> 
    </Style> 
    <ContextMenu x:Key="ContactMenu" ItemsSource="{Binding Contacts}" > 
     <ContextMenu.ItemTemplate> 
      <DataTemplate> 
       <MenuItem Header="{Binding Path=FirstName}"/> 
      </DataTemplate> 
    </ContextMenu> 
</UserControl.Resources> 

UPDATE : 나는 그것을 알아 낸

, 내가 바인딩 경로가 올바르지로 인한 전문 수집을했다.

감사합니다.

+0

지금 당장 답을 드릴 수는 없지만 –

+2

호세, 실종 된 것 같습니다. 알아 냈기 때문에 기쁩니다. 내가 여기 (그리고 다른 사람들이 그렇게 enlightented 수 있도록 솔루션을 게시 할 수 :) –

답변

2

컨텍스트 메뉴가 페이지의 시각적 트리 내에 존재하지 않으므로 데이터 컨텍스트를 상속하지 않습니다. ContextMenu에서 DataContext를 직접 설정해보십시오.

+0

예, 수정했습니다. –

1

나는 그가 문제를 어떻게 우회했는지 Jose의 대답을 놓치고 있었지만, 혼자서 알아낼 수있었습니다.

내게는 접근자를 사용한 뷰 모델 클래스에 모델을 래핑하는 것이 도움이되었습니다. 예를 들어

:

ObservableCollection<CtxItemViewModel> ctxItems = new ObservableCollection<CtxItemViewModel>(); 
CtxItem c = new CtxItem(); 
c.Name = "Hello World"; 
ctxItems.Add(new CtxItemViewModel(c)); 

와 뷰 모델 내부 : 접근 자 추가

public string Name { 
    get { return _model.Name; } 
    set { _model.Name = value; } 
} 

바인딩 저를 도왔다. 희망이 다른 사람들에게도 도움이됩니다.

관련 문제