this.Grid.CellRenderers.Remove("MultiColumnDropDown");
this.Grid.CellRenderers.Add("MultiColumnDropDown", new CustomMultiColumnDropDownRenderer());
public class CustomMultiColumnDropDownRenderer: GridCellMultiColumnDropDownRenderer
{
protected override SfMultiColumnDropDownControl OnCreateEditUIElement()
{
return new CustomMultiColumnDropDownControl(this.DataGrid);
}
protected override bool ShouldGridTryToHandleKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var multiDropDown = (this.CurrentCellRendererElement as CustomMultiColumnDropDownControl);
multiDropDown.AddItem(multiDropDown);
}
return base.ShouldGridTryToHandleKeyDown(e);
}
}
public class CustomMultiColumnDropDownControl : SfMultiColumnDropDownControl
{
SfDataGrid DataGrid;
public CustomMultiColumnDropDownControl(SfDataGrid dataGrid) : base()
{
DataGrid = dataGrid;
}
public override void OnApplyTemplate()
{
Editor = GetTemplateChild("PART_TextBox") as TextBox;
if (Editor != null)
Editor.LostFocus += Editor_LostFocus;
base.OnApplyTemplate();
}
private void Editor_LostFocus(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(SearchText))
{
AddItem(this);
}
}
internal void AddItem(SfMultiColumnDropDownControl multiDropDown)
{
var textBox = multiDropDown.GetType().GetProperty("Editor", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance).GetValue(multiDropDown);
var newValue = textBox != null ? (textBox as TextBox).Text : null;
var underlyingItems = ((multiDropDown as CustomMultiColumnDropDownControl).DataGrid.DataContext as ViewModel).ProductList;
if (underlyingItems != null)
{
bool newRecord = underlyingItems.Any(x => x.Code == newValue.ToString());
if (!newRecord)
{
underlyingItems.Add(new ProductInfo() { Code = newValue.ToString(), ID = underlyingItems.Count + 1 });
multiDropDown.SelectedItem = underlyingItems.Last();
}
}
}
} |
public class CustomMultiColumnDropDownRenderer : GridCellMultiColumnDropDownRenderer
{
protected override SfMultiColumnDropDownControl OnCreateEditUIElement()
{
return newCustomMultiColumnDropDownControl(DataGrid);
}
public override voidOnInitializeDisplayElement(DataColumnBase dataColumn, TextBlock uiElement, object dataContext)
{
var gridColumn = dataColumn.GridColumn asGridMultiColumnDropDownList;
var itemsSource = gridColumn.ItemsSource asObservableCollection<ProductInfo>;
var record = dataContext as OrderInfo;
if (!(itemsSource.Any(info => info.Code == record.ProductCode)))
itemsSource.Add(new ProductInfo() { Code = record.ProductCode });
base.OnInitializeDisplayElement(dataColumn, uiElement, dataContext);
}
protected override boolShouldGridTryToHandleKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
var multiDropDown = (CurrentCellRendererElement asCustomMultiColumnDropDownControl);
multiDropDown.AddItem(multiDropDown);
}
returnbase.ShouldGridTryToHandleKeyDown(e);
}
} |
private void Grid_Loaded(object sender, RoutedEventArgs e)
{
var itemsSource = (this.Grid.Columns["ProductCode"] as GridMultiColumnDropDownList).ItemsSource as ObservableCollection<ProductInfo>;
foreach (var item in this.Grid.View.Records)
{
var record = item.Data as OrderInfo;
if (!(itemsSource.Any(info => info.Code == record.ProductCode)))
itemsSource.Add(new ProductInfo() { Code = record.ProductCode });
}
} |
protected override void InitializeCellStyle(DataColumnBase dataColumn, object record)
{
base.InitializeCellStyle(dataColumn, record);
var gridColumn = dataColumn.GridColumn as GridMultiColumnDropDownList;
var itemsSource = gridColumn.ItemsSource as ObservableCollection<ProductInfo>;
var record1 = record as OrderInfo;
if (!(itemsSource.Any(info => info.Code == record1.ProductCode)))
{
itemsSource.Add(new ProductInfo() { Code = record1.ProductCode });
base.UpdateBindingInfo(dataColumn, record, false);
}
} |