2017-02-10 7 views
0

問題の解決策が見つかりません自動生成されたデータグリッドコンボボックスセル内のアイテムソースをプログラムで更新する方法

私は、データコレクションをオブジェクトコレクションにバインドしています。私のオブジェクトプロパティの1つがコレクションのインデックスとして使用されます。自動生成されたコンボボックスの "タイプ"列には、この索引に関連付けられた "ラベル"が表示されます。

同じオブジェクト内のインデックスである別のプロパティを更新する必要があります。私はAutogeneratingColumnイベントにコンボボックスを追加するには、このコードを使用します。

public partial class LedTableEditor : MetroWindow, INotifyPropertyChanged 
    { 
    private object _sender; 
    public Dictionary<int, string> IdColors = new Dictionary<int, string>(); 
    public ObservableCollection<Xceed.Wpf.Toolkit.ColorItem> MarshallingColors = new ObservableCollection<Xceed.Wpf.Toolkit.ColorItem>(); 
    private Dictionary<int, string> cbTypeVals = new Dictionary<int, string>(); 
    private Dictionary<UInt16, string> cbSrcValueVals = new Dictionary<UInt16, string>(); 
    private Dictionary<UInt16, string> cbDiagVals = new Dictionary<UInt16, string>(); 

    private List<string> ListeData = new List<string>(); 
    private List<string> ListeDiag = new List<string>(); 

    private int maxLeds = 16; 
    private XapLedVals led; 

    public LedTableEditor(object senderParam) 
    { 
     ----- 
    } 



private void DgLedTable_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
{ 

     if (e.PropertyName == "Type") 
     { 
      DataGridComboBoxColumn cbType = new DataGridComboBoxColumn(); 
      cbType.EditingElementStyle = new Style(typeof(ComboBox)) 
      { 
       Setters = 
       { 
        new EventSetter(Selector.SelectionChangedEvent, new SelectionChangedEventHandler(OnComboBoxSelectionChanged)) 
       } 
      }; 

      e.Column = cbType; 
      cbType.ItemsSource = cbTypeVals; // new List<string> { eLedType.ALERTE.ToString(), eLedType.DIAG.ToString(), eLedType.TRIGGER.ToString() }; 
      cbType.DisplayMemberPath = "Value"; 
      cbType.SelectedValuePath = "Key"; 
      cbType.SelectedValueBinding = new Binding("Type"); 
      e.Column.Header = "Type"; 
      e.Column.CellStyle = new Style(typeof(DataGridCell)); 
      e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)); 

     } 

     if (e.PropertyName == "Binding") 
     { 
      DataGridComboBoxColumn cbBinding = new DataGridComboBoxColumn(); 
      BindingOperations.SetBinding(cbBinding, DataGridComboBoxColumn.ItemsSourceProperty, new Binding("Source") { Source = this }); 

      e.Column = cbBinding; 
      cbBinding.ItemsSource = cbSrcValueVals; 
      cbBinding.DisplayMemberPath = "Value"; 
      cbBinding.SelectedValuePath = "Key"; 
      cbBinding.SelectedValueBinding = new Binding("Binding"); 
      e.Column.Header = "Binding"; 
      e.Column.CellStyle = new Style(typeof(DataGridCell)); 
      e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)); 
     } 
---- 
    } 

コンボボックス「タイプ」項目のソースは辞書です:私はこの「バインド」コンボボックスアイテムのソースを更新する必要が

private Dictionary<int, string> cbTypeVals = new Dictionary<int, string>(); 

、同じデータグリッドにあまりにも自動生成された:

if (e.PropertyName == "Binding") 
{ 
    AutoCommitComboBoxColumn cb = new AutoCommitComboBoxColumn(); 
    e.Column = cb; 
    cb.ItemsSource = cbSrcValueVals; 
    cb.DisplayMemberPath = "Value"; 
    cb.SelectedValuePath = "Key"; 
    cb.SelectedValueBinding = new Binding("Binding"); 
    e.Column.Header = "Binding"; 
    e.Column.CellStyle = new Style(typeof(DataGridCell)); 
    e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)); 
    } 

項目ソースは辞書である「バインド」コンボボックス:

private Dictionary<UInt16, string> cbSrcValueVals = new Dictionary<UInt16, string>(); 

それはこの1つを使用して更新する必要があります。

private Dictionary<UInt16, string> cbDiagVals = new Dictionary<UInt16, string>(); 

イベントが正常に起動されますが、私は、コンボボックスの項目を「結合」を更新する方法を見つけることができません。

private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    DgLedTable.CurrentCell = new DataGridCellInfo(DgLedTable.SelectedIndex, DgLedTable.Columns[1]); 
} 

ありがとうございました。ここ

編集コンボボックス内の項目で画面のイメージは「タイプ」である:

enter image description here

編集、コードがプロパティの追加、変更:

private System.Collections.IEnumerable _source; 
    public System.Collections.IEnumerable Source 
    { 
     get { return _source; } 
     set { _source = value; OnPropertyChanged(); } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     // DgLedTable.CurrentCell = new DataGridCellInfo(DgLedTable.SelectedIndex, DgLedTable.Columns[1]); 
     Source = cbDiagVals; 
    } 
+0

だから、あなたが「cbSrcValueVals」から「バインド」コンボボックスののItemsSourceを変更したいです他のコレクション、そう?新しいItemsSourceとしてどのコレクションを使用したいですか? – mm8

+0

はい、ちょうどコンボボックスタイプが2に設定されている場合、コンボボックスを別の辞書に更新する必要があります:private Dictionary cbDiagVals = new Dictionary (); ' – Alexus

答えて

1

を使用すると、ソースプロパティを作成した場合"バインディング" ComboBoxItemsSourceプロパティをこのオブジェクトにバインドします。このプロパティは、 "Type" ComboBox'sSelectionChangedイベントハンドラの新しいコレクションに設定できます。

ウィンドウ - またはあなたのコードが定義されているどんなタイプ - これが機能するためINotifyPropertyChangedインタフェースを実装する必要があります

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    private Dictionary<int, string> cbTypeVals = new Dictionary<int, string>(); 
    private Dictionary<UInt16, string> cbSrcValueVals = new Dictionary<UInt16, string>(); 
    private Dictionary<UInt16, string> cbDiagVals = new Dictionary<UInt16, string>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     _source = cbTypeVals; 
     //... 
    } 

    private System.Collections.IEnumerable _source; 
    public System.Collections.IEnumerable Source 
    { 
     get { return _source; } 
     set { _source = value; OnPropertyChanged(); } 
    } 


    private void OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e) 
    { 
     if (e.PropertyName == "Binding") 
     { 
      AutoCommitComboBoxColumn cb = new AutoCommitComboBoxColumn(); 
      e.Column = cb; 

      //bind the ItemsSource property to the Source property of the window here... 
      cb.SetBinding(AutoCommitComboBoxColumn.ItemsSourceProperty, new Binding("Source") { Source = this }); 

      cb.ItemsSource = cbSrcValueVals; 
      cb.DisplayMemberPath = "Value"; 
      cb.SelectedValuePath = "Key"; 
      cb.SelectedValueBinding = new Binding("Binding"); 
      e.Column.Header = "Binding"; 
      e.Column.CellStyle = new Style(typeof(DataGridCell)); 
      e.Column.CellStyle.Setters.Add(new Setter(DataGridCell.HorizontalAlignmentProperty, HorizontalAlignment.Stretch)); 
     } 
     //... 
    } 

    private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     //set the value of the Source property to a new collection and raise the PropertyChanged event here... 
     Source = cbDiagVals; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    //... 
} 
+1

'cbBinding.ItemsSource = cbSrcValueVals;'コード行を削除するのを忘れました...毎回リセットされました。あなたの助けとエレガントなソリューション...そして忍耐力をありがとう! – Alexus

+0

私は別の質問があります。ウィンドウを再オープンすると、どのように "バインディング"コンボのソースを設定できますか?デフォルトでは、 "Binding" ComboBoxは最初のdictionnary cbSrcValueValsで設定されます。 – Alexus

+1

追加のコメントをここで削除します。 @Alexus、別の質問がある場合は、[Ask Question](// stackoverflow.com/questions/ask)ボタンを[mcve]と一緒にクリックしてください。新しい質問でこの質問を参照できますが、投稿する前に関連する投稿をチェックしてみてください。 –

関連する問題