0

我正在尝试找到最优雅的方式来绑定到 Windows 窗体上的组合框,而无需使用后面的代码。在表单上,​​我有一个带有两个绑定源的组合框。一个绑定到一个控制器,该控制器包含一个国家列表,并在调用时填充此列表。另一个绑定源绑定到绑定到控制器的另一个绑定源,我已将数据成员设置为“国家/地区”的集合。我正在使用 EF5 在控制器中获取我的数据。但是,根据我所做的,当我运行我的应用程序时,我无法填充组合框。请协助我在数据绑定方面缺少什么?请在下面找到我的代码:

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            IUnityContainer container = new UnityContainer();
            Modules.RegisterModules(container);

            var controller = container.Resolve<IController>();
            Application.Run(new TestForm(controller));
        }
    }

    internal static class Modules
    {
        public static void RegisterModules(IUnityContainer container)
        {
            container.RegisterType<IController, GeoController>();
            container.RegisterType<IModelContainer, GeoModelContainer>();
        }
    }

    public class GeoController : IController, INotifyPropertyChanged
    {
        private List<Continent> _continents;
        private List<Country> _countries;
        private List<City> _cities;
        IModelContainer Container { get; set; }

        public GeoController(IModelContainer container)
        {
            Container = container;

            Countries = Container.Countries.ToList();
        }

        #region Properties    

        public List<Country> Countries
        {
            get { return _countries; }
            set
            {
                _countries = value;
                OnPropertyChanged();
            }
        }

        #endregion


        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public interface IController
    {
        List<Country> Countries { get; set; }
    }

    public partial class TestForm : Form
    {
        IController Controller { get; set; }
        public TestForm(IController controller)
        {
            InitializeComponent();

            Controller = controller;
        }
    }
4

0 回答 0