Reputation: 296
I have a datagrid binded to an object collection. one of my object property is used as an index in a collection. The autogenerated combobox column "Type" displays the "label" associated to this index.
This combobox update another property which is an index too in that same object.
The combobox "Type" item source is a dictionary : cbTypeVals
the combobox "Binding" item sources are dictionaries : cbSrcValueVals and cbDiagVals
Here is the code:
public partial class LedTableEditor : MetroWindow, INotifyPropertyChanged
{
private object _sender;
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 System.Collections.IEnumerable _source;
public System.Collections.IEnumerable Source
{
get { return _source; }
set { _source = value; OnPropertyChanged(); }
}
public LedTableEditor(object senderParam)
{
InitializeComponent();
_sender = senderParam;
DgLedTable.ItemsSource = (_sender as MainWindow)._document.Config.ListeLedTable.ListeLedTable;
cbTypeVals.Add(0, eLedType.ALERTE.ToString());
cbTypeVals.Add(1, eLedType.DIAG.ToString());
cbTypeVals.Add(2, eLedType.TRIGGER.ToString());
ListeData = (_sender as MainWindow)._document.Config.ListeDataTable.ListeDataTable.Select(x => x.Name).ToList();
if (ListeData.Find(x => x == "OFF") == null) ListeData.Insert(0, "OFF");
foreach (string s in ListeData) if(s != "") cbSrcValueVals.Add((UInt16)ListeData.IndexOf(s), s);
ListeDiag = (_sender as MainWindow)._document.Config.ListeDiagTable.ListeDiagTable.Select(x => x.Name).ToList();
if (ListeDiag.Find(x => x == "OFF") == null) ListeDiag.Insert(0, "OFF");
foreach (string s in ListeDiag) if (s != "") cbDiagVals.Add((UInt16)ListeDiag.IndexOf(s), s);
if (this.DgLedTable.Items.Count > maxLeds) this.DgLedTable.CanUserAddRows = false;
DgLedTable.Visibility = Visibility.Visible;
_source = cbSrcValueVals;
}
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;
}
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));
}
----
}
When combobox "Type" SelectionChange event is fire, I update Combobox "Binding" Itemsource :
private void OnComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if ((sender as ComboBox).SelectedIndex == 1)
Source = cbDiagVals;
else
Source = cbSrcValueVals;
}
Everything is working fine when I change ComboBox "Type" selection, but I need to update each combobox "Type" when I open Led Table, by default "Source" is set to cbSrcValueVals in ctor.
Thank you.
Link to original question : How to update programmatically the items source in an autogenerated datagrid combobox cell
Upvotes: 0
Views: 79