我和this有类似的问题,但由于没有明确回答,我想再问一次。我有一个TreeView
,ObservableCollection
通过HierarchicalDataTemplate
绑定到ContextMenu
。我基本上重新创建了一个类似于文件资源管理器的文件夹结构,并且有一个Property
,其中包含Cut,Paste等选项。我希望只有当我有一个剪切对象时才启用粘贴选项,所以我将它绑定到{ {1}}的{1}}。
简化XAML:
Window
窗口实现<Window x:Name="mainWindow">
<TabControl>
<TabItem x:Name="tabNotes" Header="Notes">
<TreeView x:Name="treeViewNotes" ItemsSource="{Binding Source={x:Static mvvm:DataSources.LoadedFolders}}" DataContext="{Binding ElementName=mainWindow}">
<TreeView.ContextMenu>
<ContextMenu>
<MenuItem Header="Paste" Click="TabNotesPaste" IsEnabled="{Binding NoteTabPasteEnabled}"/>
</ContextMenu>
</TreeView.ContextMenu>
<TreeView.Resources>
<HierarchicalDataTemplate ItemsSource="{Binding Children}" DataType="{x:Type dataclasses:NoteFolder}">
<StackPanel>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="Paste" Click="TabNotesPaste" IsEnabled="{Binding NoteTabPasteEnabled}"/>
</ContextMenu>
</StackPanel.ContextMenu>
<Label Content="{Binding Title}"/>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</TabItem>
</TabControl>
</Window>
:
INotifyPropertyChanged
对于public partial class MainWindow : Window, INotifyPropertyChanged
{
private bool _noteTabFolderPasteEnabled = false;
public bool NoteTabPasteEnabled { get { return _noteTabFolderPasteEnabled; } set { _noteTabFolderPasteEnabled = value; OnMyPropertyChanged(() => NoteTabPasteEnabled); } }
//...
}
的{{1}},它可以正常工作,但对于ContextMenu
的{{1}}则不行。我怎样才能实现它?