添加MenuItems时XamlParseException未处理
本文关键字:未处理 XamlParseException MenuItems 添加 | 更新日期: 2023-09-27 18:26:33
在实现这个问题的答案时,我遇到了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);
}
我为两个TreeViewItems
加上两个MenuItems
,就像这样。如果我编译并运行第一个集合,程序运行良好,但在将新的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>
我在XAML
:中的TreeView
<TreeView Name="Tree_One" ItemsSource="{Binding Tree}" HorizontalAlignment="Left" Background="White" SelectedItemChanged="Tree_One_SelectedItemChanged" />
您的问题在这一行:
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
。使用它并尝试检查发送事件的项目,以删除相应的项目;)