1

I have a class as below.

namespace WpfApplication2
{
    class TaskItem
    {
        private string name;
        private string item;
        private string description;
        private int priority;
        public string Name
        {
            get
            { return name; }
            set
            { this.name = value; }
        }

        public string Item
        {
            get
            {
                return item;
            }
            set
            {
                this.item = value;
            }
        }
        public string Description
        {
            get
            {
                return String.Format("{0} {1}",name,item);
            }
        }
        public int Priority
        {
            get
            {
                return priority;
            }
            set
            {
                this.priority = value;
            }
        }

        public TaskItem()
        {


        }

    }
}

I want to return the list of this class object to my wpf application as below.

<WpfApplication2:TaskItem x:Key="taskItem"/>

After that I will bind this to ListBox. How could I return this list of class object? Please advise. Thanks.

1
  • More information on what you're trying to do would be helpful in providing an answer that meets your end-goal... rather than one that just technically works. But yet I've an answer for you :) Commented Mar 24, 2011 at 3:43

3 Answers 3

1

Your xaml declaration is for a single TaskItem, not a collection. You need a collection to bind to the ItemsSource of your ListBox. See the notes on msdn about the limitations of a xaml only solution using ObservableCollection. You will probably want to create a list in code to bind to the ListBox, or create your own class that inherits from ObservableCollection and then create an instance of that class in you xaml instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Actually, I want to create two data template and will select the template using the TemplateSelector.
@David, you still need to be working with a collection of items at some point as the TemplateSelector will be applied to each of the items in the underlying collection.
Yes, this msdn link is exactly what I am looking for. Thanks a lot !
0

Have you tried to create a List<TaskItems> and set this as the DataContext ?

Comments

0

Technically possible.. with something similar to this (Replace Observable collection with List and my:Person with your custom class). See also: Generics support was added recently to XAML

<Window>
    <Window.DataContext>
        <ObservableCollection x:TypeArguments="my:Person">
            <my:Person FirstName="Tom" LastName="Holiday" />
            <my:Person FirstName="Joan" LastName="Holiday" />
        </ObservableCollection>
    </Window.DataContext>
    …
</Window>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.