我以为我会编辑它,因为我似乎误解了这个问题
您可以将其绑定为如下形式:
public partial class Form1 : Form
{
Recepie pancakes = new Recepie();
IList<UniqueHolder> items = new List<UniqueHolder>();
public Form1()
{
InitializeComponent();
pancakes.Ingredients.Add(new Ingredient { Title = "Milk - 250 gr" });
pancakes.Ingredients.Add(new Ingredient { Title = "Butter - 25 gr" });
pancakes.Ingredients.Add(new Ingredient { Title = "Oil - 1 large spoon" });
pancakes.Ingredients.Add(new Ingredient { Title = "Sugar - 100 gr" });
pancakes.Ingredients.Add(new Ingredient { Title = "Flower - 200 gr" });
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
for (var i = 0; i < pancakes.Ingredients.Count; i++)
{
Ingredient ing = pancakes.Ingredients[i];
TextBox tb = new TextBox { Location = new Point(10, i * 30), Size = new Size(200, 20), Text = ing.Title };
UniqueHolder uh = new UniqueHolder { Ingredient = ing, TextBox = tb };
this.Controls.Add(tb);
}
}
}
唯一的持有者对成分或文本框的变化进行数据绑定
public class UniqueHolder : IDisposable
{
public Guid UniqueID { get; set; }
public override bool Equals(object obj)
{
if (obj is UniqueHolder)
{
return Guid.Equals(((UniqueHolder)obj).UniqueID, this.UniqueID);
}
return base.Equals(obj);
}
public override int GetHashCode()
{
return UniqueID.GetHashCode();
}
private TextBox textbox;
public TextBox TextBox
{
get
{
return textbox;
}
set
{
if (object.Equals(textbox, value))
{
return;
}
if (textbox != null)
{
textbox.TextChanged -= OnTextChanged;
}
textbox = value;
if (textbox != null)
{
textbox.TextChanged += OnTextChanged;
}
}
}
private Ingredient ingredient;
public Ingredient Ingredient
{
get
{
return ingredient;
}
set
{
if (object.Equals(ingredient, value))
{
return;
}
if (ingredient != null)
{
ingredient.PropertyChanged -= OnIngredientChanged;
}
ingredient = value;
if (ingredient != null)
{
ingredient.PropertyChanged += OnIngredientChanged;
}
}
}
public UniqueHolder()
{
this.UniqueID = Guid.NewGuid();
}
protected virtual void OnIngredientChanged(object sender, PropertyChangedEventArgs e)
{
if (string.Equals(e.PropertyName, "Title", StringComparison.OrdinalIgnoreCase))
{
if (TextBox == null)
{
return;
}
TextBox.Text = Ingredient.Title;
}
}
protected virtual void OnTextChanged(object sender, EventArgs e)
{
var tb = sender as TextBox;
if (tb == null)
{
return;
}
if (Ingredient == null)
{
return;
}
Ingredient.Title = tb.Text;
}
public void Dispose()
{
Ingredient = null;
TextBox = null;
}
}
您可以使用这些成分回到接收器
public class Recepie : IDisposable
{
private readonly IList<Ingredient> ingredients = new ObservableCollection<Ingredient>();
public IList<Ingredient> Ingredients
{
get
{
return ingredients;
}
}
protected virtual void OnIngredientsListChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.OldItems != null)
{
foreach (var item in e.OldItems)
{
var ing = item as Ingredient;
if (ing == null)
{
continue;
}
ing.Recepie = null;
}
}
if (e.NewItems != null)
{
foreach (var item in e.NewItems)
{
var ing = item as Ingredient;
if (ing == null)
{
continue;
}
ing.Recepie = this;
}
}
}
public Recepie()
{
var obs = Ingredients as INotifyCollectionChanged;
if (obs != null)
{
obs.CollectionChanged += OnIngredientsListChanged;
}
}
public void Dispose()
{
int total = Ingredients.Count;
for (int i = total; --i >= 0; )
{
Ingredients.RemoveAt(i);
}
var obs = Ingredients as INotifyCollectionChanged;
if (obs != null)
{
obs.CollectionChanged -= OnIngredientsListChanged;
}
}
}
public class Ingredient : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private Recepie recepie;
public virtual Recepie Recepie
{
get
{
return recepie;
}
set
{
if (object.Equals(recepie, value))
{
return;
}
recepie = value;
RaisePropertyChanged("Recepie");
}
}
private string title;
public string Title
{
get
{
return title;
}
set
{
if (string.Equals(title, value))
{
return;
}
title = value;
RaisePropertyChanged("Title");
}
}
protected virtual void RaisePropertyChanged(string propertyName)
{
var local = PropertyChanged;
if (local != null)
{
local.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}