/// <summary> /// Gets a <see cref="T:Windows.UI.Xaml.Controls.ComboBox"/> control that is bound to the column's ItemsSource collection. /// </summary> /// <param name="cell">The cell that will contain the generated element.</param> /// <param name="dataItem">The data item represented by the row that contains the intended cell.</param> /// <returns>A new <see cref="T:Windows.UI.Xaml.Controls.ComboBox"/> control that is bound to the column's ItemsSource collection.</returns> protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { EnsureColumnBinding(dataItem); EnsureDisplayMemberPathExists(); EnsureItemsSourceBinding(); var comboBox = new ComboBox { Margin = default(Thickness), HorizontalAlignment = HorizontalAlignment.Stretch, VerticalAlignment = VerticalAlignment.Center }; if (dataItem != null) { var value = dataItem.GetType().GetProperty(Binding.Path.Path).GetValue(dataItem); var selection = !string.IsNullOrEmpty(DisplayMemberPath) ? ItemsSource?.Cast <object>().FirstOrDefault(x => x.GetType().GetProperty(Binding.Path.Path).GetValue(x).Equals(value)) : ItemsSource?.Cast <object>().FirstOrDefault(x => x.Equals(value)); comboBox.SelectedItem = selection; } var itemsSourceBinding = new Binding { Source = this, Path = new PropertyPath(DATAGRIDCOMBOBOXCOLUMN_itemsSourceName) }; var displayMemberPathBinding = new Binding { Source = this, Path = new PropertyPath(DATAGRIDCOMBOBOXCOLUMN_displayMemberPathName) }; comboBox.SetBinding(ComboBox.ItemsSourceProperty, itemsSourceBinding); comboBox.SetBinding(ComboBox.DisplayMemberPathProperty, displayMemberPathBinding); if (DependencyProperty.UnsetValue != ReadLocalValue(DataGridComboBoxColumn.FontFamilyProperty)) { comboBox.FontFamily = FontFamily; } if (_fontSize.HasValue) { comboBox.FontSize = _fontSize.Value; } if (_fontStyle.HasValue) { comboBox.FontStyle = _fontStyle.Value; } if (_fontWeight.HasValue) { comboBox.FontWeight = _fontWeight.Value; } RefreshForeground(comboBox, (cell != null & cell.OwningRow != null) ? cell.OwningRow.ComputedForeground : null); comboBox.SelectionChanged += (sender, args) => { var item = args.AddedItems.FirstOrDefault(); if (item != null) { var newValue = !string.IsNullOrEmpty(DisplayMemberPath) ? item.GetType().GetProperty(Binding.Path.Path).GetValue(item) : item; if (dataItem != null) { dataItem.GetType().GetProperty(Binding.Path.Path).SetValue(dataItem, newValue); } else { var dataType = OwningGrid.ItemsSource.GetItemType(); var newDataItem = Activator.CreateInstance(dataType); dataType.GetProperty(Binding.Path.Path).SetValue(newDataItem, newValue); dataItem = newDataItem; } } }; return(comboBox); }
internal DataGridCellEventArgs(DataGridCell dataGridCell) { Debug.Assert(dataGridCell != null, "Expected non-null dataGridCell parameter."); this.Cell = dataGridCell; }
protected override FrameworkElement GenerateEditingElement(DataGridCell cell, object dataItem) { return(null); }