this is unfortunately a very beginner question:
I am doing the very simple WPF tutorials and I am stuck on a namespace problem. I want to do a simple hierarchical treeview binding on a custom object according to the tutorial. I put the object into a custom namespace "MyNameSpace" and declared this in XAML ( xmlns:MyTree="clr-namespace:MyNameSpace"). I believe I don't need to specify the assembly as I am just in my project without any further reference (new and clean project).
The problem I have now, is that the compiler gives me an error at
<HierarchicalDataTemplate DataType="{x:Type MyTree:MenuItemNew}"
with the message
The name "MenuItemNew" does not exist in the namespace "clr-namespace:MyNameSpace"
But it does exist! AND it even compiles and starts the program correctly. However, I cannot see the layout anymore because of "Invalid Markup".
So how can I tell XAML to accept my namespace? Or what would be a best way to solve this?
Here is the XAML:
<Window x:Class="TreeViewTestC.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MyTree="clr-namespace:MyNameSpace"
Title="MainWindow" Height="350" Width="525">
<Grid Margin="10">
<TreeView Name="trvMenu">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type MyTree:MenuItemNew}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Title}" />
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
and here is my MainWindow Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Collections.ObjectModel;
using MyNameSpace;
namespace TreeViewTestC
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MenuItemNew root = new MenuItemNew() { Title = "Menu1" };
MenuItemNew childItem1 = new MenuItemNew() { Title = "Child item #1" };
childItem1.Items.Add(new MenuItemNew() { Title = "Child item #1.1" });
childItem1.Items.Add(new MenuItemNew() { Title = "Child item #1.2" });
root.Items.Add(childItem1);
root.Items.Add(new MenuItemNew() { Title = "Child item #2" });
trvMenu.Items.Add(root);
}
}
}
namespace MyNameSpace
{
public class MenuItemNew
{
public MenuItemNew()
{
this.Items = new ObservableCollection<MenuItemNew>();
}
public string Title { get; set; }
public ObservableCollection<MenuItemNew> Items { get; set; }
}
}