通知bindingSource.position.在Winforms中使用2种方式绑定来选择当前记录 [英] notifying bindingSource.position. Using 2 way binding in winforms to select the current record

查看:84
本文介绍了通知bindingSource.position.在Winforms中使用2种方式绑定来选择当前记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在winforms中有一个bindingSource以及一个控制器类. 我希望能够使用2种方式从控制器类中设置所选记录. 也就是说,如果正在显示表单,并且我在控制器中设置了SelectedPerson,则bindingSOurce应该使该人成为当前记录.

I have a bindingSource in winforms as well as a controller class. I want to be able to set the selected record from within the controller class using 2 way binding. That is If the form is displaying and I set the SelectedPerson in the controller then the bindingSOurce should make that person the current record.

我的控制器代码是

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

I have a bindingSource on my form and set bindingSource.DataSource = controller.BlvPersons

如果我使用控制器更新数据值,我将在表格中看到这些更改. 但是我无法解决如何在控制器中设置当前记录并查看表单中的更改.

If I Update data values using the controller I will see these changes in the form. However I cant work out how to set the current record in the controller and see the change in the form.

推荐答案

您可以使用将位置属性添加到Find方法的结果中.

You can use BindingSource.Find method and set the Position property to the results of the Find method.

仅当基础列表为 具有搜索功能的IBindingList.此方法仅指 请求到基础列表的IBindingList.Find方法.

The Find method can only be used when the underlying list is an IBindingList with searching implemented. This method simply refers the request to the underlying list's IBindingList.Find method.

要在通用的BindingList上实现搜索,需要执行多个步骤.首先,您必须通过覆盖SupportsSearchingCore属性来指示支持搜索.接下来,您必须实现

To implement search on a generic BindingList requires various steps. First, you have to indicate that searching is supported by overriding the SupportsSearchingCore property. Next, you have to implement the IBindingList.Find method, which performs the search.

您可以使用此处 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆