我有一個WinForms中的bindingSource以及一個控制器類。 我希望能夠使用2路綁定來設置控制器類中的選定記錄。 這就是如果窗體正在顯示,並且我在控制器中設置了SelectedPerson,那麼bindingSOurce應該使該人成爲當前記錄。通知bindingSource.position。在WinForms中使用2路綁定來選擇當前記錄
我的控制器代碼是
public class PeopleController : BaseController
{
private SortableBindingList<Person> _blvPersons;
public SortableBindingList<Person> BlvPersons
{
get
{
return this._blvPersons;
}
set
{
this._blvPersons = value;
this.SendChange("BlvPersons");
}
}
private Person _selectedPerson;
public Person SelectedPerson
{
get
{
return this._selectedPerson;
}
set
{
this._selectedPerson = value;
this.SendChange("SelectedPerson");
this.SendChange("BlvPersons");
this.Trace("## SelectedPerson = {0}", value);
}
}
public void InitBindingList
{
using (var repo = new PeopleRepository(new OrganisationContext()))
{
IList<Person> lst = repo.GetList(p => p.Id > 0 && p.Archived == false, x => x.Organisation);
this.BlvPersons = new SortableBindingList<Person>(lst);
} }
}
//ect
}
public class BaseController : INotifyPropertyChanged, IDisposable
{
public event PropertyChangedEventHandler PropertyChanged;
public void SendChange(string propertyName)
{
System.Diagnostics.Debug.WriteLine("PropertyChanged {0} = {1}", propertyName, GetType().GetProperty(propertyName).GetValue(this, null));
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
// etc
我有我的窗體上的BindingSource,並設置bindingSource.DataSource = controller.BlvPersons
如果我使用控制器更新數據值,我會看到這些變化形成。 但是我不能解決如何設置控制器中的當前記錄並查看錶單中的更改。
其實我希望我不必與控制器共享BindingSource。這是唯一的方法嗎?它不會真的具有約束力。或者,也許該位置不能被綁定... – 2014-08-29 00:23:33
我不認爲這是可能的。在WinForms中,BindingSource充當表單和數據之間的中介。 – 2014-08-29 01:37:09
@jacobseleznez,我想出了一個想法,似乎工作。幾天後我會改變它的答案。如果你同意它,你可能會提供它作爲答案:-) – 2014-08-29 18:43:21