2014-09-30 1 views
1

나는이 (필자는 executed/canexecute 핸들러가 호출 의미 "작업"에 의해) MenuItem의 작동 (A Window에서) 일부 CommandBindings,MenuItem에서 WPF CommandBindings을 손상시킬 수있는 것은 무엇입니까?

I의 (a UserControl에) 다른 사람이있는에 할당하는 작업 CommandButton (처리기가 호출 됨)의 속성 - MenuItem과 함께 사용되는 경우 (처리기가 호출되지 않음).

<MenuItem 
    Header="Select All" 
    Command="{StaticResource SelectAllCommand}" 
    > 
    <MenuItem.CommandBindings> 
     <CommandBinding 
      Command="{StaticResource SelectAllCommand}" 
      Executed="SelectAll_Executed" 
      CanExecute="SelectAll_CanExecute" 
      /> 
    </MenuItem.CommandBindings> 
</MenuItem> 

을하지만 그 바보 (아래 참조)

나는 복사하여 UserControlCommandBinding와 올바르게 MenuItem 상호 작용을하고과 같이, 적절한 MenuItem.CommandBindings에 각각 결합을 붙여 넣기 할 수 있습니다.

나는 또한 그들이 생성자 '는 UserControl의 창으로 최대의 명령 바인딩'을 UserControl을 복사하여 작업 할 수 있습니다 :

C# 다시

Application.Current.MainWindow.CommandBindings.AddRange(this.CommandBindings); 

, 그건 꽤 미친 짓이야,하지만 않습니다 문맥 적 요인이 여기에 작용한다는 것을 암시하는 것처럼 보입니다.

컨트롤 XAML의 관련 비트를 다음 테스트 XAML로 복사하여 문제를 재현했지만 문제가 재현되지 않았습니다. 내가 추출한 프로덕션 코드와 달리, 예상대로 작동합니다. 바인딩, 바인딩 된 메서드를 호출합니다. 그러나 menuitem에 대한 명령에 이벤트 처리기 메서드를 바인딩하는 동일한 방법은 UserControl 다른 및 incompably vaster 및 더 복잡한) 프로젝트에서 실패합니다.

는 XAML :

<UserControl 
    x:Class="CommandTest.TestControl" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="300" 
    > 
    <UserControl.Resources> 
     <ResourceDictionary> 
      <RoutedUICommand x:Key="TestCommand" /> 

      <ContextMenu x:Key="TestMenu"> 
       <MenuItem 
        Header="_Test" 
        Command="{StaticResource TestCommand}" 
        /> 
      </ContextMenu> 
     </ResourceDictionary> 
    </UserControl.Resources> 

    <UserControl.CommandBindings> 
     <CommandBinding 
      Command="{StaticResource TestCommand}" 
      Executed="TestCommand_Executed" 
      CanExecute="TestCommand_CanExecute" 
      /> 
    </UserControl.CommandBindings> 

    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition /> 
      <RowDefinition /> 
     </Grid.RowDefinitions> 

     <TextBox 
      Width="200" 
      ContextMenu="{StaticResource TestMenu}" 
      /> 
    </Grid> 
</UserControl> 

C 번호 :

private void TestCommand_Executed(object sender, ExecutedRoutedEventArgs e) 
{ 
    MessageBox.Show("Test Command", "Test", MessageBoxButton.OK, 
        MessageBoxImage.Information); 
} 

private void TestCommand_CanExecute(object sender, CanExecuteRoutedEventArgs e) 
{ 
    e.CanExecute = true; 
    e.Handled = true; 
} 

그래서 질문은, 숨겨진 어떤 요인은 CommandBinding가 자동으로 실패 할 수있다? 어떻게 이것을 디버깅합니까? 동일한 컨트롤에서 동일한 명령 바인딩에 대해 ButtonMenuItem 사이에서 완전히 다른 동작이 나타나는 이유는 무엇입니까? ContextMenu이 (가) 리소스인가요? 그러나 테스트 코드에서는 리소스이며 모든 것이 작동합니다.

UPDATE :

Another solution^Wworkaround는 : 명시 적으로 ContextMenuMenuItem.PlacementTarget 설정합니다. 허.

답변

1

테스트 프로젝트와 프로덕션 프로젝트의 시각적 트리가 명령 라우팅을 방해하고 있습니다. 라우팅 된 명령은 시각적 트리를 따라 명령 처리기를 검색하며 최대 두 개의 경로가 사용될 수 있습니다. this MSDN Magazine article에서

는 :

일반적으로 명령 호출자는 자신의 시각적 트리의 위치와 시각적 트리의 루트 사이의 바인딩 명령을 찾습니다.하나가 발견되면 바운드 명령 핸들러는 명령이 사용 가능한지 판별하고 명령이 호출 될 때 호출됩니다. 명령이 툴바 또는 메뉴 (또는 일반적으로 FocusManager.IsFocusScope = true를 설정하는 컨테이너) 내부의 컨트롤에 연결되면 루트에서 포커스까지 시각적 트리 경로를 따라 보이는 추가 논리 실행 명령 바인딩을위한 요소.

관련 문제