Samuel Brockmann
Samuel Brockmann

Reputation: 113

XAML ObjectDataProvider, ObjectType Error

I'm a trying to instantiate an object (that was created in C#) in XAML using the ObjectDataProvider. Unfortunately, I'm receiving the following error: "The type reference cannot find a public type named 'TYPENAME'". I have a .cs file of the same name as the the TYPENAME.

Here's my XAML:

<Window x:Class="PROJECTNAME.PROJECTFILE"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
        xmlns:local="clr-namespace:PROJECTNAME"
        Title="PROJECTFILE" Height="500" Width="500">
    <Window.Resources>
        <ResourceDictionary>
            <!-- Line 9 is the one right below this one. -->
            <ObjectDataProvider x:Key="NAME1" ObjectType="{x:Type TYPENAME}"/> 
            <ObjectDataProvider x:Key="NAME2" ObjectInstance="{StaticResource TYPENAME}" MethodName="METHODNAME"/>
        </ResourceDictionary>
    </Window.Resources>
    <Grid Height="375">
        <DockPanel DataContext="{Binding Source={StaticResource TYPENAME}}" Width="440" Margin="10,20,191,35">
            <dg:DataGrid Name="DG" ItemsSource="{Binding}"/>
        </DockPanel>
        <DockPanel Width="85" Height="25" Margin="0,350,0,0">
            <Frame Name="Frame"/>
            <Button Content="See Posts Info" Click="Button_Click"/>
        </DockPanel>
    </Grid>
</Window>

Here's the C# file for TYPENAME:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace PROJECTNAME
{
    class TYPENAME
    {
        private AccesDBDataSet data_set;
        private AccesDBDataSetTableAdapters.tblTYPENAMETableAdapter TYPENAMEAdapter;

        public TYPENAME()
        {
            data_set = new AccesDBDataSet();
            DataTable tblTYPENAME = data_set.Tables[1];
            TYPENAMEAdapter = new AccesDBDataSetTableAdapters.TYPENAMEAdapterTableAdapter();
            TYPENAMEAdapter.Fill(data_set.tblTYPENAMEAdapter);
        }

        public DataView METHODNAME()
        {
            return data_set.tblTYPENAMEAdapter.DefaultView;
        }
    }
}

So, why exactly is TYPENAME unrecognized in line 9 of the XAML? Am I formatting it incorrectly? I tried setting it up as

<ObjectDataProvider x:Key="NAME1" ObjectType="{x:Type local:TYPENAME}"/> 

but that caused the same bug.

Thanks for the help!

Upvotes: 2

Views: 2654

Answers (1)

Abe Heidebrecht
Abe Heidebrecht

Reputation: 30498

I see a couple of errors. You need to tell the XAML processor the fully qualified type name. Since it doesn't understand C# namespaces, you have to use a clr-namespace declaration (you have one, but you aren't using it). Second, your StaticResource should be pointing to the x:Key attribute on the first ObjectDataProvider. This should work:

<ObjectDataProvider x:Key="NAME1" ObjectType="{x:Type local:TYPENAME}"/>
<ObjectDataProvider x:Key="NAME2" ObjectInstance="{StaticResource NAME1}" MethodName="METHODNAME"/>

Note also that your Binding has an incorrect StaticResource as well. Since I'm not sure which ObjectDataProvider you want to use with it, I didn't pick one. But you will need to use either NAME1 or NAME2 as the key for the StaticResource.

Edit for using clr-namespace

There are two ways to use a clr-namespace:

  • if the namespace is from the same assembly the XAML is defined in:

    xmlns:local="clr-namespace:MyNamespace"

  • if the namespace is from a different assembly (you must reference the other assembly in your project where the XAML is being used):

    xmlns:local="clr-namespace:MyNamespace;assembly=MyAssemblyName"

In your case, it looks like both types are in the same assembly, so you'd use the first method of doing so. If you're getting an error that the type can not be found, it is likely because it needs to be a public type, and yours is internal (the default protection level for types in C#).

Just for emphasis, I'll quote that MSDN article:

clr-namespace: The CLR namespace declared within the assembly that contains the public types to expose as elements.

Upvotes: 3

Related Questions