WPF를 처음 사용합니다. 다른 많은 사람들과 마찬가지로 ContextMenu
을 ObservableCollection
에 바인딩하여 동적 컨텍스트 메뉴를 만들려고합니다. 메뉴 항목을 나타내는 MenuItemViewModel
클래스의 TheCommand
속성에 Command
속성을 바인딩하는 것을 제외하면 모두 작동합니다. 이 명령은 실행되지 않습니다. 내가 도대체 뭘 잘못하고있는 겁니까?동적 MVVM 컨텍스트 메뉴에서 명령 바인딩이 작동하지 않습니다.
처음부터 시작하려면 ContextMenu
은 Image
의 하위이며 마우스가 Image
위에있을 때 표시됩니다. 빈의 ContextMenu는 다음과 같이 정의된다
<Image.ContextMenu >
<ContextMenu ItemsSource="{DynamicResource ContextMenu}"
은 다음과 같습니다
<Window.Resources>
<local:MenuItemViewModelCollection x:Key="ContextMenu">
</local:MenuItemViewModelCollection>
<HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"
ItemsSource="{Binding Path=Children}">
<HierarchicalDataTemplate.ItemContainerStyle>
<Style TargetType="MenuItem">
<Setter Property="Command"
Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},
Path=DataContext.TheCommand}"/>
<!-- Value="{Binding Path=TheCommand}" /> I tried this too -->
</Style>
</HierarchicalDataTemplate.ItemContainerStyle>
</HierarchicalDataTemplate>
</Window.Resources>
TheCommand
속성은 아래에 정의되어
public class MenuItemViewModel : INotifyPropertyChanged
{
//...
public ICommand TheCommand
{
//...
}
}
'MenuItemViewModelCollection' 클래스는 어떻게 생겼습니까? 메뉴 항목이 올바르게 표시됩니까? – Rachel