none
"Type reference cannot find public type named ..." RRS feed

  • Question

  • Hi

    I'm just trying to make my first WPF app. I'm currently trying to bind an ObservableCollection to a ListView. It all works correctly - my data is displatey as I want. However, in the Designer window I get an error in the errors window:

    "Type reference cannot find public type named DataItem", and the Designer window wont display.

    Code below.


    <Window x:Class="My_Namespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:
    My_Namespace"
        Title="Window" Height="500" Width="600" Cursor="Arrow" ToolTip="Window" WindowStartupLocation="CenterScreen"
        MinWidth="60" MinHeight="50">
        <Window.Resources>
            <ObjectDataProvider x:Key="ItemCollection" ObjectType="{x:Type my:ItemCollection}" />
            <DataTemplate DataType="{x:Type myBig SmileataItem}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding FilePath}" />
                </StackPanel>
            </DataTemplate>
        </Window.Resources>

        <Grid>
            <ListView ItemsSource="{Binding Source={StaticResource ItemCollection}}" Name="m_view">
            </ListView>
        </Grid>
    </Window>


    namespace
    My_Namespace
    {
    public partial class MainWindow : Window
        {       
            public MainWindow()
            {
                          
                InitializeComponent();           
            }
        }

        public partial class DataItem
        {
            private String m_path;

            public
    DataItem(String path)
            {
                m_path = path;
            }

            public String FilePath
            {
                get { return m_path; }
                set { m_path = value; }
            }
        }

        public partial class ItemCollection : ObservableCollection<DataItem>
        {
            public
    ItemCollection ()
            {
                base.Add(new DataItem("Hello"));
                base.Add(new 
    DataItem("World"));
            }
        }
    }


    I have a feeling it might be my 'my' namespace declaration but I'm not sure.

    Please help, this is driving me nuts!

    Wednesday, December 5, 2007 7:06 PM

Answers

  • Turns out _Ed Noepel may have been on the right track. I got just went on and did some other stuff to do with my program, came across this site

    http://www.beacosta.com/blog/?cat=9

    I need to do some of things that Beatriz does here, anyway I used her DataTemplates - they look like this.


    <DataTemplate x:Key=”NameTemplate”>
                <TextBlock Text=”{Binding XPath=@Name}” FontWeight=”Bold” (…) />
            </DataTemplate>



    And the problem has magically gone away now! Yays

    Thanks for your suggestions.
    Wednesday, December 5, 2007 11:35 PM

All replies

  • WPF doesn't like nested types.  For your DataTemplate, there is no XAML syntax such as DataType="{x:Type my:MainWindow.DataItem}" that would make this work as intended.  It works at runtime because you are not using that DataTemplate anywhere.

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2307430&SiteID=1


    Wednesday, December 5, 2007 7:32 PM
  • I wouldn't say that DataType="{x:Type my:MainWindow.DataItem}"

    DataType="{x:Type my:MainWindow.DataItem}" is the problem!!! I think that's ok...

     

    Did you try reloading the designer?

    Wednesday, December 5, 2007 8:28 PM
  • I'm not sure what you mean. My class DataItem is not nested inside the MainWindow class - they are both within the same namespace. Also, how come ItemCollection works fine but DataItem does not work?

    Also, I think the DataTemplate is used - this displays the string twice:

    <DataTemplate DataType="{x:Type myBig SmileataItem}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding FilePath}" />
    <TextBlock Text="{Binding FilePath}" />
                </StackPanel>
            </DataTemplate>
    Wednesday, December 5, 2007 8:30 PM
  • I've tried reloading the designer, closing visual studio. I actually started this yesterday and my machine has been off since then.
    Wednesday, December 5, 2007 8:33 PM
  • I know that this might sound really not nice but what i would suggest is to start commenting parts of the XAML until the designer starts working again... just to know where is the issue...

    I would assume that this is a bug in VS2008... The wpf designer can be improved...

    Wednesday, December 5, 2007 8:38 PM
  • The problem stems from this line:

    <DataTemplate DataType="{x:Type myBig SmileataItem}">

    Also, it would be incredible if this is a bug - why release a product with a bug that relates to one of major selling points of WFP, namely databinding?
    Wednesday, December 5, 2007 8:49 PM
  • Turns out _Ed Noepel may have been on the right track. I got just went on and did some other stuff to do with my program, came across this site

    http://www.beacosta.com/blog/?cat=9

    I need to do some of things that Beatriz does here, anyway I used her DataTemplates - they look like this.


    <DataTemplate x:Key=”NameTemplate”>
                <TextBlock Text=”{Binding XPath=@Name}” FontWeight=”Bold” (…) />
            </DataTemplate>



    And the problem has magically gone away now! Yays

    Thanks for your suggestions.
    Wednesday, December 5, 2007 11:35 PM
  • For referecing nested type in WPF, this should work:
    <DataTemplate DataType="{x:Type my:MainWindow+DataItem}"/>

    More information can be found in the following blog post:
    http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!503.entry

    As a sidenote, from Ian's comment, this is not actually a "undocumented" behaviour.

    Hope this helps
    Monday, December 10, 2007 6:48 AM
  • I  find the solution to this, and just to remind myself, that in my application, I encouter this same problem, and at last I find the reason to this is, that we should not put any spaces in our asembly name.
    Tony Zeng qzcbs@163.com
    • Proposed as answer by Tony Zeng Thursday, May 26, 2011 6:21 AM
    Thursday, May 26, 2011 6:21 AM
  •  

    Here is one more solution to the problem:

    http://visualizationtools.net/default/error102-the-type-reference-cannot-find-a-public-type-named/

    If you referring to the your custom control like this :

    xmlns:local="clr-namespace:CustomControl;assembly=CustomControl"

    Change the declaration as follows:

    xmlns:local="clr-namespace:CustomControl"

     

    Every thing should works fine after this. This is a common problem you face when you add the reference  to the custom control class in visual studio intellisense. 

     

    Monday, October 10, 2011 12:05 PM