0

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.
Blank ContextMenu MenuItem

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
Groger
  • 532
  • 3
  • 15
  • Have you stepped through the `SelectedType` property to see what's returning when the context menu is built? It could be returning `string.Empty`? – Tronald Nov 04 '19 at 16:19
  • I think you should create the ContextMenu at the top of your xaml with x:Key, assign it to StackPanel ContextMenu through `StaticResource` and then use the `ElementName` Binding. Chances are this is in a `DataTemplate`, which is not part of Visual Tree. Also check the Output window and see if you have any errors. – XAMlMAX Nov 04 '19 at 16:22
  • @Tronald The property is not called when `ContextMenu` is shown, however, other calls to it return the correct value. Also, because of the `StringFormat`, even if the property returns `string.Empty` the `MenuItem` should still contain text. – Groger Nov 04 '19 at 16:25
  • @XAMlMAX I'm trying what you recommended. If the format is incorrect, let me know. `ContextMenu="{StaticResource Template}"` produces the same result as before. `ContextMenu="{Binding ElementName=Template}"` prevents the `ContextMenu` from appearing. – Groger Nov 04 '19 at 16:48
  • `"{Binding ElementName=Page, Path=ChildType, StringFormat='Add New {0}'}"` what is `Page` here ? can you show the XAML for that. – Abin Nov 04 '19 at 16:50
  • @AbinMathew `Page` is the name of the complete WPF page, which contains the `StackPanel` and various other controls. I use this convention to bind to properties in the code behind, such as `SelectedType`. – Groger Nov 04 '19 at 16:54
  • Check for binding errors. I have a strong feeling that the visual tree of `Page` is not part of your `ContextMenu` – Abin Nov 04 '19 at 17:00
  • @Groger “Page is the name of the complete WPF page” isn’t valid XAML. Can you edit your question and provide the information Abin requested? Thanks. – 15ee8f99-57ff-4f92-890c-b56153 Nov 04 '19 at 17:05
  • 1
    The duplicate isn't an exact match, unless you want to set `DataContext=this;` in the window's constructor (not a great idea as a rule, but harmless with a window). But it explains what the problem is, and the general approach to fixing it. – 15ee8f99-57ff-4f92-890c-b56153 Nov 04 '19 at 17:17
  • 1
    Perhaps preferable: Add this to the StackPanel: `Tag="{Binding RelativeSource={RelativeSource AncestorType=Window}}"` and bind Header as follows: `Header="{Binding PlacementTarget.Tag.SelectedType, StringFormat='Copy {0}', RelativeSource={RelativeSource AncestorType=ContextMenu}}"`. – 15ee8f99-57ff-4f92-890c-b56153 Nov 04 '19 at 17:20
  • 1
    @EdPlunkett Your last comment made progress. The `MenuItem` now displays the value of the property, however it does not format correctly according to `StringFormat`. – Groger Nov 04 '19 at 17:33
  • 1
    @Groger Oh right, try HeaderStringFormat for the header. – 15ee8f99-57ff-4f92-890c-b56153 Nov 04 '19 at 17:47

0 Answers0