XAML ObjectDataProvider, ObjectType Error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rocksam2003
    New Member
    • Aug 2013
    • 4

    XAML ObjectDataProvider, ObjectType Error

    I'm a trying to instantiate an object (that was created in C#) in XAML using the ObjectDataProvi der. 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:
    Code:
    <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>
                <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:
    Code:
    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
    Code:
    <ObjectDataProvider x:Key="NAME1" ObjectType="{x:Type local:TYPENAME}"/>
    but that caused the same bug.

    Thanks for the help!
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    I'm not sure why you are seeing the error but I noticed that your source is incorrect for your DockPanel in your XAML. You are attempting to bind to a Static Resource called TYPENAME but the key for that resource is actually NAME2.

    Try fixing your binding and see if it fixes the problem.
    If it doesn't I'll try and reproduce the problem here.

    -Frinny
    Last edited by Frinavale; Sep 5 '13, 01:07 PM.

    Comment

    • rocksam2003
      New Member
      • Aug 2013
      • 4

      #3
      That actually didn't fix it.

      Thanks, though; you saved me an error later one, I'm sure.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        I'm not sure what you are doing wrong because I cannot reproduce it.

        I added a window to my project called ProjectFile (same as yours except not in all caps).

        I then added a class called TypeName and used the normal DataSet instead of the access one since I don't have that available.

        Lastly, I copied and pasted your XAML markup and made some binding adjustments to get it to work properly (as per the corrections I suggested before).

        I am unable to reproduce the problem that you are having.

        This is my XAML
        Code:
        <Window x:Class="ProjectFile"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:ProjectName"
                Title="ProjectFile" Height="300" Width="300">
            <Window.Resources>
                <ResourceDictionary>
                    <ObjectDataProvider x:Key="NAME1" ObjectType="{x:Type local:TypeName}"/>
                    <ObjectDataProvider x:Key="NAME2" ObjectInstance="{StaticResource NAME1}" MethodName="MethodName"/>
                </ResourceDictionary>
            </Window.Resources>
            <Grid Height="375">
                <DockPanel DataContext="{Binding Source={StaticResource NAME2}}" Width="440" Margin="10,20,191,35">
                    <DataGrid Name="DG" ItemsSource="{Binding}"/>
                </DockPanel>
                <DockPanel Width="85" Height="25" Margin="0,350,0,0">
                    <Frame Name="Frame"/>
                    <Button Content="See Posts Info" />
                </DockPanel>
            </Grid>
        </Window>
        This is my code for the TypeName class:

        (VB)
        Code:
        Class TypeName
            Private data_set As System.Data.DataSet
            Public Sub New()
                data_set = New System.Data.DataSet()
                Dim tblTypeName As New System.Data.DataTable
                tblTypeName.Columns.Add("Col1")
                tblTypeName.Columns.Add("Col2")
                For i As Integer = 0 To 10
                    Dim dr As System.Data.DataRow = tblTypeName.NewRow
                    dr(0) = String.Format("{0} ", i.ToString)
                    dr(1) = String.Format("{0} ", i.ToString)
                    tblTypeName.Rows.Add(dr)
                Next
                data_set.Tables.Add(tblTypeName)
            End Sub
        
            Public Function MethodName() As System.Data.DataView
                Return data_set.Tables(0).DefaultView
            End Function
        End Class
        (C#)
        Code:
        class TypeName
        {
        	private System.Data.DataSet data_set;
        	public TypeName()
        	{
        		data_set = new System.Data.DataSet();
        		System.Data.DataTable tblTypeName = new System.Data.DataTable();
        		tblTypeName.Columns.Add("Col1");
        		tblTypeName.Columns.Add("Col2");
        		for (int i = 0; i <= 10; i++) {
        			System.Data.DataRow dr = tblTypeName.NewRow();
        			dr[0] = string.Format("{0} ", i.ToString());
        			dr[1] = string.Format("{0} ", i.ToString());
        			tblTypeName.Rows.Add(dr);
        		}
        		data_set.Tables.Add(tblTypeName);
        	}
        
        	public System.Data.DataView MethodName()
        	{
        		return data_set.Tables[0].DefaultView;
        	}
        }
        The only thing that I can think that is going wrong is that something didn't compile properly and so you are getting a strange error.

        -Frinny
        Last edited by Frinavale; Sep 5 '13, 01:06 PM.

        Comment

        Working...