views:

202

answers:

1

I have a class with an ObservableCollection called List and I am trying to bind to textboxes individually. I have been trying:

<TextBox Text="{Binding Source=List[0], Path=Value}" />
<TextBox Text="{Binding Source=List[1], Path=Value}"/>

The StringObject class is just:

class StringObject
{
    public string Value { get; set; }
}

Can someone advise?

+2  A: 

If this is for a WPF app.

Given this code behind:

/// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
            this.DataContext = new ListCon();
        }
    }

    public class ListCon
    {
        public List<StringObject> List
        {
            get
            {
                var list = new List<StringObject>();
                list.Add(new StringObject() { Value = "Hello World" });
                return list;
            }
        }
    }

    public class StringObject
    {
        public string Value { get; set; }
    }

The binding would look like this:

<TextBox Text="{Binding List[0].Value}" />
Nathan W
Thank you very much.
JP