나는이 (필자는 executed
/canexecute
핸들러가 호출 의미 "작업"에 의해) MenuItem
의 작동 (A Window
에서) 일부 CommandBindings
,MenuItem에서 WPF CommandBindings을 손상시킬 수있는 것은 무엇입니까?
I의 (a UserControl
에) 다른 사람이있는에 할당하는 작업 Command
Button
(처리기가 호출 됨)의 속성 - MenuItem
과 함께 사용되는 경우 (처리기가 호출되지 않음).
<MenuItem
Header="Select All"
Command="{StaticResource SelectAllCommand}"
>
<MenuItem.CommandBindings>
<CommandBinding
Command="{StaticResource SelectAllCommand}"
Executed="SelectAll_Executed"
CanExecute="SelectAll_CanExecute"
/>
</MenuItem.CommandBindings>
</MenuItem>
을하지만 그 바보 (아래 참조)
나는 복사하여 UserControl
에 CommandBinding
와 올바르게 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
가 자동으로 실패 할 수있다? 어떻게 이것을 디버깅합니까? 동일한 컨트롤에서 동일한 명령 바인딩에 대해
Button
과
MenuItem
사이에서 완전히 다른 동작이 나타나는 이유는 무엇입니까?
ContextMenu
이 (가) 리소스인가요? 그러나 테스트 코드에서는 리소스이며 모든 것이 작동합니다.
UPDATE :
Another solution^Wworkaround는 : 명시 적으로 ContextMenu
에 MenuItem.PlacementTarget
설정합니다. 허.