2011-06-11 3 views
2

WPF 응용 프로그램을 만들고 MVVM 패턴을 따르고 있습니다. 내 xaml에 컨텍스트 메뉴가 있고 명령과 헤더 텍스트를 바인딩해야합니다. 다음 코드를 사용하면 컨텍스트 메뉴의 헤더를 관찰 가능한 컬렉션 인 BOList의 속성 인 "MenuItemName"과 바인딩 할 수 있습니다. 내 문제는 명령이 해고되지 않는다는 것입니다. 나는 상황에 맞는 메뉴의 항목 소스가 명령이 잘 작동하지만 내 헤더를 비워지고MVVM을 사용하여 WPF + 바인딩 컨텍스트 메뉴 항목의 명령 및 헤더

(DataContext="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}") 

데이터 컨텍스트로 변경됩니다. 내 머리글과 메뉴 항목의 명령을 바인딩하는 방법이 있습니까? 여기에서 MenuClick 명령은 VM의 Icommand 속성이고 MenuItemName은 ListBox에 바인딩 된 관찰 가능한 컬렉션 인 BOList 내부의 속성입니다. 상황에 맞는 메뉴를 사용 DataContext를 대신 다음

+0

MenuItemName하지만이 뷰 모델처럼 무엇을 보는가? Beinding Expression 오류에 대한 콘솔 출력을보고, 표시되면 내용을 게시하십시오. – Berryl

답변

0

은 :

<MenuItem Header="{Binding Path=PlacementTarget.MenuItemName, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}" Command="{Binding Path=PlacementTarget.MenuClick, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}" /> 
+0

datacontext가 설정된 경우 명령을 실행했지만 항목 속성이 설정된 경우 헤더 속성 바인딩이 실패합니다. 헤더 바인딩이 작동하지만 명령 바인딩이 실패하면 – user793886

+1

안녕하세요. 컨텍스트 메뉴의 Item Source를 BOList (observable collection)로 설정하면 icommand 정의가 Window data ciontext (vm)에 있기 때문에 명령이 실행되지 않습니다. 현명한 코드를 처리해야합니다. 디버깅은 바인딩이 불가능하기 때문에, 나는 부시 주위를 때리고있었습니다. :-)이 링크는 저에게 많은 도움을주었습니다 http://www.wpftutorial.net/DebugDataBinding.html – user793886

0

이 시도 메뉴 항목 바인딩 항목을 소스
를 사용하여

<Grid> 
<StackPanel Orientation="Vertical"> 
    <Button x:Name="btnClickMe" Command="{Binding ButtonCommand}" Content="Click Me" /> 
    <ListBox ItemsSource="{Binding BOList}" x:Name="lstDemo" SelectedItem="{Binding BussinessObj,Mode=OneWayToSource}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
     <StackPanel x:Name="stkStyleRender" Orientation="Horizontal" Background="Cyan" Width="525" Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType={x:Type ListBox}}}" > 
      <TextBlock x:Name="txtId" Text="{Binding FirstName}"></TextBlock> 
      <TextBlock x:Name="txtName" Text="{Binding LastName}"></TextBlock> 
      <StackPanel.ContextMenu> 
      <ContextMenu x:Name="cntMnuTest" ItemsSource ="{Binding Path=PlacementTarget.Tag, RelativeSource={RelativeSource Self}}" > 
       <MenuItem Header="{Binding MenuItemName}" Command="{Binding MenuClick}" CommandParameter="Icon"></MenuItem> 
      </ContextMenu> 
      </StackPanel.ContextMenu> 

     </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
</StackPanel> 

+0

그게 저에게 맞지 않습니다 ... – user793886

+0

만약 u는 DataContext를 설정 한 다음 bindinng 명령을 실행하고 항목 소스 헤더 바인딩을 설정하면 .. ??? user793886

+0

자세히 설명해 주시겠습니까? –

1

안녕하세요 문제를 확인했다.

ICommand 정의가 Window 데이터 컨텍스트 (VM)에 있기 때문에 컨텍스트 메뉴의 항목 소스를 BOList (관찰 가능한 수집) 명령으로 설정하지 않으면 명령이 실행되지 않습니다.

현명한 코드를 처리해야합니다. 디버깅 이후

는 바인딩 할 수 없습니다, 내가

이 링크 :-) 수풀 주위에 구타 한 것은 나에게 도움이 많이 WPF Tutorial - Debug Databinding Issues in WPF

관련 문제