Я являюсь новым в привязке данных.
У меня есть те классы:
public class Foo : List<Bar>
{
public string FooName { get; set; }
}
public class Bar
{
public string BarName { get; set; }
public string BarDesc { get; set; }
}
И у меня есть a List<Foo>
Я хотел бы иметь Foo
объекты в ComboBox
, и Bar
объекты в ListBox
. Когда я изменяю выбранный пункт в ComboBox
, Я хочу ListBox
измениться. Когда я изменяю выбранный пункт в ListBox
Я хотел бы иметь TextBox
заполненный BarDesc
.
Следующие работы только для ListBox
и ComboBox
:
comboBox1.DataSource = foos;
comboBox1.DisplayMember = "FooName";
listBox1.DataBindings.Add("DataSource", foos, "");
listBox1.DisplayMember = "BarName";
Я не делаю теперь, как связать выбранный Bar
в ListBox
кому: TextBox.Text
свойство. Возможно, добавленная привязка для listBox1
не хорошая идея.
Возможно, я должен сделать что-то как этот:
((CurrencyManager)listBox1.BindingContext[foos]).CurrentChanged += new EventHandler((o, a) =>
{
textBox1.DataBindings.Clear();
textBox1.DataBindings.Add("Text", listBox1.DataSource, "BarDesc");
});
Как я могу разрешить свою проблему?
Чтобы выполнить всю эту работу, мне пришлось добавить свойство Items
в класс Foo
. Это «связь / отношения» между двумя источниками привязки.
public partial class Form1 : Form {
public class Foo : List<Bar> {
public string FooName { get; set; }
public Foo(string name) { this.FooName = name; }
public List<Bar> Items { get { return this; } }
}
public class Bar {
public string BarName { get; set; }
public string BarDesc { get; set; }
public Bar(string name, string desc) {
this.BarName = name;
this.BarDesc = desc;
}
}
public Form1() {
InitializeComponent();
List<Foo> foos = new List<Foo>();
Foo a = new Foo("letters");
a.Add(new Bar("a", "aaa"));
a.Add(new Bar("b", "bbb"));
foos.Add(a);
Foo b = new Foo("digits");
b.Add(new Bar("1", "111"));
b.Add(new Bar("2", "222"));
b.Add(new Bar("3", "333"));
foos.Add(b);
//Simple Related Object List Binding
//http://blogs.msdn.com/bethmassi/archive/2007/04/21/simple-related-object-list-binding.aspx
BindingSource comboBoxBindingSource = new BindingSource();
BindingSource listBoxBindingSource = new BindingSource();
comboBoxBindingSource.DataSource = foos;
listBoxBindingSource.DataSource = comboBoxBindingSource;
listBoxBindingSource.DataMember = "Items";
comboBox1.DataSource = comboBoxBindingSource;
comboBox1.DisplayMember = "FooName";
listBox1.DataSource = listBoxBindingSource;
listBox1.DisplayMember = "BarName";
textBox1.DataBindings.Add("Text", listBoxBindingSource, "BarDesc");
}
}
Используйте BindingSource для всех ваших сложных потребностей в связывании данных:
// bind to List<Foo>
BindingSource comboBoxBindingSource = new BindingSource();
comboBoxBindingSource.DataSource = foos;
// use this binding source in comboBox1
// for display use FooName
comboBox1.DataSource = comboBoxBindingSource;
comboBox1.DisplayMember = "FooName";
// bind to comboBox1's SelectedValue
// it will point to the Foo selected in combobox
BindingSource listBoxBindingSource = new BindingSource();
listBoxBindingSource.DataSource = comboBox1;
listBoxBindingSource.DataMember = "SelectedValue";
// use this binding source in listBox1
// for display use BarName
listBox1.DataSource = listBoxBindingSource;
listBox1.DisplayMember = "BarName";
// bind to comboBox'1s SelectedValue (reusing listBoxBindingSource)
// and set Text to value of BarDesc
textBox1.DataBindings.Add("Text", listBoxBindingSource, "BarDesc");
Вам, вероятно, следует установить ValueMember в комбинированном списке и списке. Возможно, вам также потребуется обработать bindingSource.CurrentChanged и повторно привязать список к текущему выбранному списку.