我想将mnuAddLessonAfter的可见性绑定到mnuAddLessonBefore的可见性。我似乎无法使其工作,即将mnuAddLessonBefore的可见性设置为折叠仅隐藏mnuAddLessonBefore,而不是mnuAddLessonAfter。我做错了什么?
<TreeView Grid.Column="0"
HorizontalAlignment="Stretch"
Name="treeViewLesson"
VerticalAlignment="Stretch"
SelectedItemChanged="treeViewLesson_SelectedItemChanged"
PreviewMouseLeftButtonDown="treeViewLesson_PreviewMouseLeftButtonDown"
AllowDrop="True"
Drop="treeViewLesson_Drop"
MouseMove="treeViewLesson_MouseMove"
DragEnter="treeViewLesson_DragEnter">
<TreeView.ContextMenu>
<ContextMenu Name="context_menu_lesson">
<MenuItem Name="mnuAddLesson"
Header="Add lesson"
Click="mnuAddLesson_Click" />
<MenuItem Name="mnuAddLessonBefore"
Header="Add lesson before"
Click="mnuAddLessonBefore_Click" />
<MenuItem Name="mnuAddLessonAfter"
Header="Add lesson after"
Click="mnuAddLessonAfter_Click"
Visibility="{Binding ElementName=mnuAddLessonBefore, Path=Visibility}" />
编辑:
我在输出控制台中注意到了这一点:
System.Windows.Data错误:4:不能 找到与参考绑定的源 '的ElementName = mnuAddLessonBefore'。 BindingExpression:路径=能见度; 的DataItem = NULL;目标元素是 'MenuItem'(Name ='mnuAddLessonAfter'); target属性是'Visibility'(类型 '能见度')
答案 0 :(得分:1)
上下文菜单很难绑定。它们存在于您的控件的可视树之外,因此无法找到您的元素名称。
尝试此解决方法
public Window1()
{
InitializeComponent();
NameScope.SetNameScope(context_menu_lesson, NameScope.GetNameScope(this));
}