1

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; }
    }
}
nvoigt
  • 75,013
  • 26
  • 93
  • 142
Sven G
  • 21
  • 6
  • What is the error you get? – adminSoftDK Jun 18 '15 at 10:19
  • 1
    Seems really silly, but did you try doing a clean and rebuild? I copied your code exactly, did a build, and the error went away. – Eric P. Jun 18 '15 at 10:46
  • I cleaned and rebuilt and it stayed. I use Visual Studio 2012, but I think without service packs. I can't easily access updates here. So the XAML is basically correct, right? – Sven G Jun 18 '15 at 11:14
  • Sometimes I run into issues like this and solve them by running "Clean Solution", followed by restarting Visual Studio, followed by building again. It is also possible that you have encountered a bug in Visual Studio that might be fixed in an update. – Xavier Jun 18 '15 at 12:12

2 Answers2

1

It seems to me that your main application namespace is TreeViewTestC and not MyNameSpace. Therefore, you may well need to tell the compiler that your MyNameSpace is actually in the TreeViewTestC assembly, despite you only having a single project. Try using this instead:

xmlns:MyTree="clr-namespace:MyNameSpace;assembly=TreeViewTestC"
Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • 1
    Seems to work fine until I clean and rebuild. Then this gives again the error "The type reference cannot find a public type named 'MenuItemNew'" – Sven G Jun 18 '15 at 14:07
  • I upgraded to Visual Studio 2013 Update 4. Still the same error. (No matter if I put assembly or not) – Sven G Jun 19 '15 at 09:37
1

Ok, I found the solution. The project was on a network drive. Moving it to a local folder solves the issues... Man, what a mess. This has cost me so much time and I need the network drive..

More info: The name "XYZ" does not exist in the namespace "clr-namespace:ABC"

Thanks a lot for your help

Community
  • 1
  • 1
Sven G
  • 21
  • 6