我想将ContextMenu
MenuItem
中的文本绑定到班级的一个属性上。我已经将其他控件绑定到该属性并获得了预期的结果。但是,绑定到MenuItem
时,ContextMenu
会生成空白项目。
类似的问题was found here,但答案无法解决我的情况,因为我已经使用了属性。
这是我的ContextMenu
<StackPanel.ContextMenu>
<ContextMenu StaysOpen="True">
<MenuItem Header="{Binding ElementName=Page,Path=ChildType,StringFormat='Add New {0}'}"/>
<MenuItem Header="{Binding ElementName=Page,Path=SelectedType,StringFormat='Copy {0}'}"/>
<MenuItem Header="{Binding ElementName=Page,StringFormat='Delete {0}'}"/>
</ContextMenu>
</StackPanel.ContextMenu>
还有我的财产
public string SelectedType
{
get
{
object selectedItem = this.ScheduleTree.SelectedItem;
if (selectedItem.GetType() == typeof(Schedule))
return "Schedule";
if (selectedItem.GetType() == typeof(Batch))
return "Batch";
if (selectedItem.GetType() == typeof(Sequence))
return "Sequence";
if (selectedItem.GetType() == typeof(Coil))
return "Coil";
return string.Empty;
}
}
就像我说的那样,此绑定在其他上下文中也可以正常运行。如何绑定MenuItem
标头以正确显示文本?
编辑:
关闭ContextMenu
会产生以下输出:
Exception thrown: 'System.ArgumentNullException' in PresentationCore.dll