1

在实施这个问题的答案期间,我遇到了XamlParseException运行时错误。

我将解释错误何时发生,因为它对我没有任何意义。首先,我使用上一个问题中的解决方案来构建两个Context Menus在用户右键单击TreeViewitems.

编辑:为了更好地理解,我将完全按照我的方式发布代码。将代码切换到此后,我仍然收到错误消息。

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);
}

我添加两个MenuItems完全像这样的两个TreeViewItems。如果我在实现第一组的情况下编译并运行,程序运行良好,但是在将 new 添加MenuItems到另一个之后TreeViewItem,我得到了那个XamlParseException was unhandled错误。这可能是什么原因造成的?

谢谢你的帮助。

XAML 按要求

<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>

TreeViewXAML

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

1 回答 1

0

您的问题出在这一行:

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

改成这样:

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

是否可以重用这样的代码?

    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);
    }

注意:您将对ContextMenu两者使用相同的。使用它并尝试检查哪个项目是发送事件以删除适当的项目;)

于 2013-08-06T14:50:16.210 回答