コードビハインドを使用せずに、Windows フォームのコンボボックスにバインドするために使用できる最もエレガントな方法を見つけようとしています。フォームには、2 つのバインディング ソースを持つコンボボックスがあります。1 つは、国のリストを含む Controller にバインドされ、呼び出されると、このリストにデータが入力されます。他のバインディング ソースは、コントローラーにバインドされた他のバインディング ソースにバインドされ、データ メンバーを 'Countries' のコレクションとして設定しました。コントローラーでデータをフェッチするために 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;
}
}