2014-08-28 31 views
0

我有一個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

如果我使用控制器更新數據值,我會看到這些變化形成。 但是我不能解決如何設置控制器中的當前記錄並查看錶單中的更改。

回答

1

您可以使用BindingSource.Find方法並將Position屬性設置爲Find方法的結果。

Find方法只能在底層列表爲 IBindingList且搜索已實現時使用。該方法只需將 請求引用到基礎列表的IBindingList.Find方法。

要在通用BindingList上實現搜索需要執行各種步驟。首先,你必須通過覆蓋SupportsSearchingCore屬性來支持搜索。接下來,您必須實施執行搜索的IBindingList.Find方法。

您可以使用herehere中的示例。

+0

其實我希望我不必與控制器共享BindingSource。這是唯一的方法嗎?它不會真的具有約束力。或者,也許該位置不能被綁定... – 2014-08-29 00:23:33

+1

我不認爲這是可能的。在WinForms中,BindingSource充當表單和數據之間的中介。 – 2014-08-29 01:37:09

+0

@jacobseleznez,我想出了一個想法,似乎工作。幾天後我會改變它的答案。如果你同意它,你可能會提供它作爲答案:-) – 2014-08-29 18:43:21

0

因爲我不想在控制器類中使用winforms引用,所以我不想在窗體和控制器之間共享bindingSource。

相反,我想出了有一個控制器中的RecordPosition屬性,並將其綁定到一個文本框

在我的窗體的想法,我有

BindHelper.BindText(this.textRecordPosition,this.controller,"RecordPosition"); 

private void textRecordPosition_TextChanged(object sender, EventArgs e) 
    { 
     this.bindingSource.Position = Convert.ToInt32(textRecordPosition.Text) -1; 
    } 

private void bindingSource_PositionChanged(object sender, EventArgs e) 
    { 
     this.controller.RecordPosition = this.bindingSource.Position + 1; 
    } 

在我的控制器我有

public int RecordPosition 
    { 
     get 
     { 
      return this._position; 
     } 

     set 
     { 
      this._position = value; 
      this.SendChange("RecordPosition"); 
     } 
    } 

在我的BindHelper類中,我有

public static void BindText(TextBox box, object dataSource, string dataMember) 
    { 
     var bind = new Binding("Text", dataSource, dataMember, true, DataSourceUpdateMode.OnPropertyChanged); 
     box.DataBindings.Add(bind); 
    } 
相關問題