绑定MenuItem标头会导致空白项目

我想将ContextMenu MenuItem中的文本绑定到班级的一个属性上。我已经将其他控件绑定到该属性并获得了预期的结果。但是,绑定到MenuItem时,ContextMenu会生成空白项目。

绑定MenuItem标头会导致空白项目

类似的问题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
leecomezhou 回答:绑定MenuItem标头会导致空白项目

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3165312.html

大家都在问