从另一个类访问一个类中的BindingSource

时间:2017-09-26 14:40:16

标签: c# scope bindingsource

我在同一个项目中有2个类,ProjectView和FeatureView。我需要从另一个类访问一个类中的BindingSource。我有一个kluge,我在BindingSource范围internal而不是private。惭愧,羞耻。有更好的方法吗?。

// ProjectView.cs
public partial class ProjectView : System.Windows.Forms.UserControl {

}

// ProjectView.Designer.cs
partial class ProjectView {
    // This should be private
    internal System.Windows.Forms.BindingSource bsFeatures;
}  

// FeatureView.cs
public partial class FeatureView : System.Windows.Forms.UserControl { 

    // Get ProjectView
    Project currentProject = this._presenter.WorkItem.State["CurrentProject"] as Infrastructure.Interface.Aml.BusinessEntities.Project;
    string key = System.String.Concat("Project", currentProject.Id);
    this._presenter.WorkItem.State["CurrentProject"] = currentProject;
    ProjectView view = _presenter.WorkItem.Items.Get<ProjectView>(key);

    // Populate currentProject.Features with ProjectView.bsFeatures.List
    currentProject.Features.Clear();
    IList featureList = view.bsFeatures.List;
    foreach (Feature feature in featureList)
    {
        currentProject.Features.Add(feature);
    }

}

1 个答案:

答案 0 :(得分:2)

也许是这样的,不确定:

    partial class ProjectView
{
    // This should be private
    private System.Windows.Forms.BindingSource bsFeatures;

    public System.Windows.Forms.BindingSource BindingSource
    {
        get { return bsFeatures; }
    }

    public void ShareOnlyWith(FeatureView  fw)
    {
        fw.BindingSource = bsFeatures;
    }
}
当然,我们打破了其中一条原则,不依赖于具体结构。