Hi all,

I am attempting data binding in WPF. I have a class Recipe and another called RecipeService. There seems a problem with the RecipeService class in terms of compatibility with the Recipe class as when I add its type to the ObjectDataProvider in the XAML file, I get an error that RecipeList type is not found. Any suggestions?

Furthermore, I am using Visual Studio.Net and the "Recipe" text in "public List<Recipe> GetRecipeList()" should be highlighted to Blue if accurate, however it is not.

Your suggestions are appreciated.

The code is below...

/*C# code*/
using System.Collections.Generic;

namespace TestApp
{
    public class Recipe
    {
        private int _id;
        private string _title;   //can be used as recipe name
        private string _mealType;

        public Recipe(int id, string title, string _mealType)
        {
            _id = id;
            _title = title;
            _mealType = mealType;
        }
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }
        public string MealType
        {
            get { return _mealType; }
            set { _mealType = value; }
        }
    }

    class RecipeService
    {
        public List<Recipe> GetRecipeList()
        {
            Recipe pasta = new Recipe(1, "Pasta", "Lunch");
            Recipe toast = new Recipe(2, "Toast", "Breakfast");
            List<Recipe> recipeList = new List<Recipe>();
            recipeList.Add(pasta);
            recipeList.Add(toast);
            return recipeList;
        }
    }
}


<!--Some part of the code in the XAML File-->
<StackPanel>
   <StackPanel.Resources>
        <ObjectDataProvider x:Key="recipes" ObjectType="{x:Type svc:RecipeService}" MethodName="GetRecipeList" ></ObjectDataProvider>

   </StackPanel.Resources>
</StackPanel>

<!--Here is the error message-->
error MC3050: Cannot find the type 'svc:RecipeService'. Note that type names are case sensitive.

Actually disregard,..please carry on with the problem as the error still persists.

Still not solved!!

Ooops .. too late :)

Actually disregard,..please carry on with the problem as the error still persists.

Ok, one more try .. my browser is playing tricks on me today, so pleas don't mark it solved till I finish my answer :D

Ok, one more source for the errors might be, if you haven't specified a namespace for your class. Here's an example for you, where similar thingy is used: Data Binding in WPF.

P.S. I may be wrong, cause I have no tools to test this .. so this is just a wild guess :)

A namespace is specified as namespace TestApp and I have the 2 classes Recipe and RecipeService as separate files. I have added both to the project via "Project" -> "Add existing item to the project" and no errors suffice, however when I proceed to bind them in the XAML, I get an error.

I add this line of code to the XAML -->

<ObjectDataProvider x:Key="recipes" ObjectType="{x:Type svc:RecipeService}" MethodName="GetRecipeList" ></ObjectDataProvider>

and I just ran it and it executes without issues...Please stay posted cause this is fairly new to me. Thanks.

Can you attach your project, so I could take a look at it this evening?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.