如何将TextBoxes绑定到Collection中的各个元素

时间:2009-08-11 05:51:41

标签: wpf data-binding textbox observablecollection

我有一个名为List的ObservableCollection类,我试图单独绑定到文本框。我一直在努力:

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

StringObject类只是:

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

有人可以提供建议吗?

1 个答案:

答案 0 :(得分:3)

如果这是针对WPF应用的。

鉴于此代码背后:

/// <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; }
    }

绑定看起来像这样:

<TextBox Text="{Binding List[0].Value}" />