在实施这个问题的答案期间,我遇到了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>
我TreeView
的XAML
:
<TreeView Name="Tree_One" ItemsSource="{Binding Tree}" HorizontalAlignment="Left" Background="White" SelectedItemChanged="Tree_One_SelectedItemChanged" />