Search code examples
c#wpfmenuitemxamlparseexception

XamlParseException was Unhandled when adding MenuItems


During implementing the answer from this question I came across the XamlParseException run-time error.

I will explain when the error occurs, because it doesn't make any sense to me. First of all, I use the solution from the previous question to build two Context Menus that come down when the user right-clicks the TreeViewitems.

EDIT: For better understanding, I'm going to post the code exactly how I have it. I am still receiving the error after switching my code to this.

public static TreeViewItem item1 = new TreeViewItem();
public static TreeViewItem item2 = new TreeViewItem();

ContextMenu item1_CM = new ContextMenu() { Background = Brushes.White, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
        MenuItem add1 = new MenuItem() { Header = "Add..." }; //Add & Delete MenuItems
        MenuItem delete1 = new MenuItem() { Header = "Delete..." };

ContextMenu item2_CM = new ContextMenu() { Background = Brushes.White, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
        MenuItem add2 = new MenuItem() { Header = "Add..." }; //Add & Delete MenuItems
        MenuItem delete2 = new MenuItem() { Header = "Delete..." };

public constructor()
{
     Tree = new ObservableCollection<TreeViewItem>();
     Tree.Add(item1);
     Tree.Add(item2);

     //Add MenuItems to TreeView ContextMenus
     item1_CM.Items.Add(add1);
     item1_CM.Items.Add(delete1);
     item1.Items.Add(item1_CM);

     item2_CM.Items.Add(add2);
     item2_CM.Items.Add(delete2);
     item2.Items.Add(item2_CM);
}

I add two MenuItems EXACTLY like this for two TreeViewItems. If I compile and run with the first set implemented, the program runs fine, but after adding new MenuItems to the other TreeViewItem, I get that XamlParseException was unhandled error. What could be causing this?

Thanks for your help.

XAML As requested

<Window x:Class="CircularButtonPrototype.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:cmp="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Title="Window1" MinHeight="685" MaxHeight="685" Height="685" MinWidth="1044" MaxWidth="1044" Width="1044" AllowsTransparency="False" WindowStartupLocation="CenterScreen" WindowStyle="None" Visibility="Visible">
</Window>

My TreeView in XAML:

<TreeView Name="Tree_One" ItemsSource="{Binding Tree}" HorizontalAlignment="Left" Background="White" SelectedItemChanged="Tree_One_SelectedItemChanged" />

Solution

  • Your problem is at this line:

     item1.Items.Add(item1_CM);
     ...
     item2.Items.Add(item2_CM);
    

    Change it by this:

    item1.ContextMenu = item1_CM;
    ...
    item2.ContextMenu = item2_CM;
    

    Is it possible to reuse code like this?

        public ObservableCollection<TreeViewItem> Tree { get; set; }
    
        public static TreeViewItem Item = new TreeViewItem {Header = "MainTreeViewItem"};
        public static TreeViewItem Item2 = new TreeViewItem {Header = "MainTreeViewItem"};
    
        ContextMenu contextMenu = new ContextMenu { Background = Brushes.White, BorderBrush = Brushes.Black, BorderThickness = new Thickness(1) };
        MenuItem addItem = new MenuItem() { Header = "Add..." }; //Add & Delete MenuItems
        MenuItem deleteItem = new MenuItem() { Header = "Delete..." };
    
        public MainWindow()
        {
            Tree = new ObservableCollection<TreeViewItem>();
            //Add MenuItems to TreeView ContextMenus
            contextMenu.Items.Add(addItem);
            contextMenu.Items.Add(deleteItem);
    
            Item.ContextMenu = contextMenu;
            Item2.ContextMenu = contextMenu;
    
            Tree.Add(Item);
            Tree.Add(Item2);
        }
    

    NOTE: you will be using the same ContextMenu for both. Use it and try to check which item is the one sending the event to delete the appropiate one ;)