在实施this question的答案时,我遇到了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);
}
会出现。{/ p>
编辑:为了更好地理解,我将准确地发布代码。将代码切换为此后,我仍然收到错误。
MenuItems
我为两个TreeViewItems
添加了两个MenuItems
。如果我编译并运行第一组实现,程序运行正常,但在将新TreeViewItem
添加到另一个XamlParseException was unhandled
后,我得到<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
XAML
中的<TreeView Name="Tree_One" ItemsSource="{Binding Tree}" HorizontalAlignment="Left" Background="White" SelectedItemChanged="Tree_One_SelectedItemChanged" />
:
{{1}}
答案 0 :(得分: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
。使用它并尝试检查哪个项目是发送事件的项目以删除适当的项目;)