0

詳細にコンボ ボックスがあるマスター詳細シナリオを設定しようとしています。純粋なデータバインディングを使用して各詳細項目の選択を保持する方法はありますか、またはここで行う必要がある手動の配管があります。

以下のサンプルの再現手順:

  1. M1の詳細を「Detail2」に変更
  2. マスター M2 に切り替えてから M1 に戻る
  3. 詳細は Detail1 に戻ります。以前に選択した値を保持することを期待しています

フォーム コンストラクター:

InitializeComponent ();

List<MasterData> dataList = new List<MasterData> ();

dataList.Add (new MasterData ("M1"));
dataList.Add (new MasterData ("M2"));

// master data
this.masterBindingSource.DataSource = typeof (MasterData);
this.masterBindingSource.DataSource = dataList;

this.masterComboBox.DataSource = this.masterBindingSource;
this.masterComboBox.DisplayMember = "Name";

// details                    
this.detailsBindingSource.DataMember = "Details";
this.detailsBindingSource.DataSource = this.masterBindingSource;

this.detailsComboBox.DataBindings.Add (new System.Windows.Forms.Binding ("SelectedValue", this.masterBindingSource,
    "SelectedDetail", true));

this.detailsComboBox.DataSource = this.detailsBindingSource;
this.detailsComboBox.DisplayMember = "Name";

// Label to make sure the master is actually changing
this.detailLabel.DataBindings.Add (new System.Windows.Forms.Binding ("Text", this.masterBindingSource, 
    "Name", true, DataSourceUpdateMode.OnPropertyChanged));

データ:

public class Details
{
    public String Name { get; set; }

    public Details (string name)
    {
        this.Name = name;
    }
}

public class MasterData
{
    public String Name { get; set; }
    public List<Details> Details { get; set; }
    public Details SelectedDetail { get; set; }

    public MasterData (string name)
    {
        this.Details = new List<Details> ();
        this.Name = name;

        this.Details.Add (new Details ("Detail1"));
        this.Details.Add (new Details ("Detail2"));
        this.SelectedDetail = this.Details.FirstOrDefault ();
    }
}

私は WPF のバックグラウンドから来ており、WinForms でのバインディングを理解するのに苦労しています。うまくいけば、これはすべて理にかなっています。

4

0 に答える 0