I want to bind the text in a ContextMenu
MenuItem
to a property in my class. I have already bound other controls to this property and achieved desired results. When binding to a MenuItem
, however, the ContextMenu
produces blank items.
A similar issue was found here, but the answer does not resolve my case, as I already use a property.
Here is my 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, Path=SelectedType, StringFormat='Delete {0}'}"/>
</ContextMenu>
</StackPanel.ContextMenu>
And my property
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;
}
}
Like I said, this binding functions properly in other contexts. How can I bind a MenuItem
header to properly display the text?
Edit:
Closing the ContextMenu
produces the following output:
Exception thrown: 'System.ArgumentNullException' in PresentationCore.dll