/// <summary> /// Find or create a grid cell. /// cell.IsEditing is true on F2 and mouse click. /// </summary> /// <param name="cell"></param> /// <param name="dataItem"></param> /// <returns></returns> protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem) { var content = new ContentControl(); IGridCellVM context = GridMappingCells.FindOrCreate(cell.Column.Header, dataItem); var binding = new Binding() { Source = context }; // cell edition mode ? on a textBox by F2 or mouse click if (cell.IsEditing) { // only for text box (not for checkbox!) if (context is GridCellValueVM) { content.ContentTemplate = CellEditingTemplate; } else { content.ContentTemplate = CellTemplate; } } else { content.ContentTemplate = CellTemplate; } content.SetBinding(ContentControl.ContentProperty, binding); return(content); }
/// <summary> /// refresh a cell, by the col and row. /// (called because of a row relation: update the linked cell. /// </summary> /// <param name="columnVM"></param> /// <param name="rowVM"></param> public void RefreshCell(IGridColumnVM columnVM, IGridRowVM rowVM) { IGridCellVM value = this.Where(x => x.RowBinding == rowVM && x.ColumnBinding == columnVM).FirstOrDefault(); if (value == null) { return; } value.Refresh(); }
/// <summary> /// Find or create the cell view model corresponding to the row and the column. /// </summary> /// <param name="columnBinding"></param> /// <param name="rowBinding"></param> /// <returns></returns> public override IGridCellVM FindOrCreate(object columnBinding, object rowBinding) { // try to find it IGridCellVM value = this.Where(x => x.RowBinding == rowBinding && x.ColumnBinding == columnBinding).FirstOrDefault(); if (value != null) { return(value); } // the value does not exists, create it return(Create(columnBinding as IGridColumnVM, rowBinding as IGridRowVM)); }
//===================================================================== #region Privates methods. /// <summary> /// Create a mapped value of a cell, between a col and a row. /// </summary> /// <param name="columnBinding"></param> /// <param name="rowBinding"></param> /// <returns></returns> private IGridCellVM Create(IGridColumnVM columnBinding, IGridRowVM rowBinding) { // find the cell in the row, by the column IGridCell cell = rowBinding.GridRow.FindCellByColumn(columnBinding.GridColumn); if (cell == null) { throw new Exception("The cell is not found in the dataGrid!"); } // create the cellVM, can be a value or a component (button, combobox,...) IGridCellVM mapValue = _gridFactory.CreateCellVM(cell); mapValue.ColumnBinding = columnBinding; mapValue.RowBinding = rowBinding; Add(mapValue); return(mapValue); }