問題の解決策が見つかりません自動生成されたデータグリッドコンボボックスセル内のアイテムソースをプログラムで更新する方法
私は、データコレクションをオブジェクトコレクションにバインドしています。私のオブジェクトプロパティの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]);
}
ありがとうございました。ここ
編集コンボボックス内の項目で画面のイメージは「タイプ」である:
編集、コードがプロパティの追加、変更:
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;
}
だから、あなたが「cbSrcValueVals」から「バインド」コンボボックスののItemsSourceを変更したいです他のコレクション、そう?新しいItemsSourceとしてどのコレクションを使用したいですか? – mm8
はい、ちょうどコンボボックスタイプが2に設定されている場合、コンボボックスを別の辞書に更新する必要があります:private Dictionary cbDiagVals = new Dictionary (); ' –
Alexus