/// <summary> /// Initialeses DataGrid And Generates Columns From The Bound ViewModels Grid Fields /// </summary> /// <param name="gridSectionViewModel"></param> /// <param name="dynamicDataGrid"></param> public static void CreateDataGrid(DynamicGridViewModel gridSectionViewModel, DataGrid dynamicDataGrid) { //want to do any service calls asynchronously dynamicDataGrid.CanUserAddRows = false; dynamicDataGrid.Columns.Clear(); if (gridSectionViewModel != null) { dynamicDataGrid.HeadersVisibility = gridSectionViewModel.DisplayHeaders ? DataGridHeadersVisibility.Column : DataGridHeadersVisibility.None; gridSectionViewModel.ApplicationController.DoOnAsyncThread(() => { if (gridSectionViewModel.FieldMetadata == null) { return; } var columnMetadata = new List <ColumnMetadata>(); foreach (var gridField in gridSectionViewModel.FieldMetadata.OrderBy(gf => gf.Order)) { var thisRecordType = gridField.AltRecordType ?? gridSectionViewModel.RecordType; var fieldName = gridField.FieldName; if (gridSectionViewModel.RecordService.FieldExists(fieldName, thisRecordType)) { var fieldMetadata = gridSectionViewModel.RecordService.GetFieldMetadata(fieldName, thisRecordType); var thisColumn = new ColumnMetadata(thisRecordType, fieldName, gridField.OverrideLabel ?? fieldMetadata.DisplayName ?? fieldName, fieldMetadata.FieldType, gridField.WidthPart, gridField.IsEditable, fieldMetadata.Description, gridSectionViewModel.GetHorizontalJustify(fieldMetadata.FieldType), gridSectionViewModel.DisplayHeaders, gridField.AliasedFieldName); columnMetadata.Add(thisColumn); } } gridSectionViewModel.ApplicationController.DoOnMainThread(() => { if (gridSectionViewModel.CanDelete) { var deleteColumn = new DeleteRowColumn(); deleteColumn.Binding = new Binding("DeleteRowViewModel"); dynamicDataGrid.Columns.Add(deleteColumn); } if (gridSectionViewModel.CanEdit) { var editColumn = new EditRowColumn(); editColumn.Binding = new Binding("EditRowViewModel"); dynamicDataGrid.Columns.Add(editColumn); } foreach (var column in columnMetadata) { var cellBinding = new Binding { Path = new PropertyPath(string.Concat("[", column.AliasedFieldName ?? column.FieldName, "]")), Mode = BindingMode.TwoWay }; DataGridColumn dataGridField; if (column.FieldType == RecordFieldType.Url) { dataGridField = new GridUrlColumn() { Binding = cellBinding }; } else if (gridSectionViewModel.IsReadOnly) { dataGridField = new GridStringDisplayOnlyColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Boolean || column.FieldType == RecordFieldType.ManagedProperty) { dataGridField = new GridBooleanColumn { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.RecordType) { dataGridField = new GridPicklistColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.RecordField) { var metadata = gridSectionViewModel.RecordService.GetFieldMetadata(column.FieldName, column.RecordType); if (metadata.IsMultiSelect) { dataGridField = new GridMultiSelectColumn() { Binding = cellBinding }; } else { dataGridField = new GridPicklistColumn() { Binding = cellBinding }; } } else if (column.FieldType == RecordFieldType.Picklist) { var metadata = gridSectionViewModel.RecordService.GetFieldMetadata(column.FieldName, column.RecordType); if (metadata.IsMultiSelect) { dataGridField = new GridMultiSelectColumn() { Binding = cellBinding }; } else { dataGridField = new GridPicklistColumn() { Binding = cellBinding }; } } else if (column.FieldType == RecordFieldType.Lookup) { if (gridSectionViewModel.FormController.FormService != null && gridSectionViewModel.FormController.FormService.UsePicklist(column.FieldName, column.RecordType)) { dataGridField = new GridLookupPicklistColumn() { Binding = cellBinding }; } else { dataGridField = new GridLookupColumn() { Binding = cellBinding }; } } else if (column.FieldType == RecordFieldType.Password) { dataGridField = new GridPasswordColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Enumerable) { dataGridField = new GridEnumerableColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Object) { dataGridField = new GridLookupPicklistColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.FileRef) { dataGridField = new GridFileRefColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Integer) { var format = gridSectionViewModel.RecordService.GetIntegerFormat(column.FieldName, column.RecordType); if (format == IntegerType.TimeZone || format == IntegerType.Language) { dataGridField = new GridIntPicklistColumn() { Binding = cellBinding }; } else { dataGridField = new GridIntColumn() { Binding = cellBinding }; } } else if (column.FieldType == RecordFieldType.BigInt) { dataGridField = new GridIntColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Date) { dataGridField = new GridDateColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Decimal) { dataGridField = new GridDecimalColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Double) { dataGridField = new GridDoubleColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Money) { dataGridField = new GridMoneyColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.ActivityParty) { dataGridField = new GridActivityPartyColumn() { Binding = cellBinding }; } else { dataGridField = new GridStringColumn() { Binding = cellBinding }; } dataGridField.Header = column; dataGridField.Width = new DataGridLength(column.WidthPart, DataGridLengthUnitType.Pixel); var isFormReadonly = gridSectionViewModel.IsReadOnly; var isWriteable = gridSectionViewModel?.RecordService?.GetFieldMetadata(column.FieldName, column.RecordType).Createable == true || gridSectionViewModel?.RecordService?.GetFieldMetadata(column.FieldName, column.RecordType).Writeable == true; dataGridField.IsReadOnly = isFormReadonly || !isWriteable; var description = gridSectionViewModel?.RecordService?.GetFieldMetadata(column.FieldName, column.RecordType).Description; dynamicDataGrid.Columns.Add(dataGridField); } var dataGridBinding = new Binding { Path = new PropertyPath("GridRecords"), Mode = BindingMode.TwoWay }; dynamicDataGrid.SetBinding(ItemsControl.ItemsSourceProperty, dataGridBinding); var selectedItemBinding = new Binding { Path = new PropertyPath("SelectedRow"), Mode = BindingMode.TwoWay }; dynamicDataGrid.SetBinding(Selector.SelectedItemProperty, selectedItemBinding); }); }); } }
/// <summary> /// Initialeses DataGrid And Generates Columns From The Bound ViewModels Grid Fields /// </summary> /// <param name="gridSectionViewModel"></param> /// <param name="dynamicDataGrid"></param> public static void CreateDataGrid(DynamicGridViewModel gridSectionViewModel, DataGrid dynamicDataGrid) { //want to do any service calls asynchronously dynamicDataGrid.CanUserAddRows = false; dynamicDataGrid.Columns.Clear(); if (gridSectionViewModel != null) { gridSectionViewModel.ApplicationController.DoOnAsyncThread(() => { if (gridSectionViewModel.FieldMetadata == null) { return; } var columnMetadata = new List <ColumnMetadata>(); foreach (var gridField in gridSectionViewModel.FieldMetadata.OrderBy(gf => gf.Order)) { var fieldName = gridField.FieldName; var fieldType = gridSectionViewModel.RecordService.GetFieldType(fieldName, gridSectionViewModel .RecordType); var header = gridSectionViewModel.RecordService.GetFieldLabel(fieldName, gridSectionViewModel.RecordType); var thisColumn = new ColumnMetadata(fieldName, header, fieldType, gridField.WidthPart, gridField.IsEditable); columnMetadata.Add(thisColumn); } gridSectionViewModel.ApplicationController.DoOnMainThread(() => { if (gridSectionViewModel.CanDelete) { var deleteColumn = new DeleteRowColumn(); deleteColumn.Binding = new Binding("DeleteRowViewModel"); dynamicDataGrid.Columns.Add(deleteColumn); } if (gridSectionViewModel.CanEdit) { var editColumn = new EditRowColumn(); editColumn.Binding = new Binding("EditRowViewModel"); dynamicDataGrid.Columns.Add(editColumn); } foreach (var column in columnMetadata) { var cellBinding = new Binding { Path = new PropertyPath(string.Concat("[", column.FieldName, "]")), Mode = BindingMode.TwoWay }; DataGridColumn dataGridField; if (column.FieldType == RecordFieldType.Boolean) { dataGridField = new GridBooleanColumn { Binding = cellBinding } } ; else if (column.FieldType == RecordFieldType.RecordType) { dataGridField = new GridPicklistColumn() { Binding = cellBinding } } ; else if (column.FieldType == RecordFieldType.RecordField) { var metadata = gridSectionViewModel.RecordService.GetFieldMetadata(column.FieldName, gridSectionViewModel.RecordType); if (metadata.IsMultiSelect) { dataGridField = new GridMultiSelectColumn() { Binding = cellBinding }; } else { dataGridField = new GridPicklistColumn() { Binding = cellBinding }; } } else if (column.FieldType == RecordFieldType.Picklist) { var metadata = gridSectionViewModel.RecordService.GetFieldMetadata(column.FieldName, gridSectionViewModel.RecordType); if (metadata.IsMultiSelect) { dataGridField = new GridMultiSelectColumn() { Binding = cellBinding }; } else { dataGridField = new GridPicklistColumn() { Binding = cellBinding }; } } else if (column.FieldType == RecordFieldType.Lookup) { if (gridSectionViewModel.FormController.FormService != null && gridSectionViewModel.FormController.FormService.UsePicklist(column.FieldName, gridSectionViewModel.RecordType)) { dataGridField = new GridLookupPicklistColumn() { Binding = cellBinding }; } else { dataGridField = new GridLookupColumn() { Binding = cellBinding }; } } else if (column.FieldType == RecordFieldType.Password) { dataGridField = new GridPasswordColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Enumerable) { dataGridField = new GridEnumerableColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Object) { dataGridField = new GridLookupPicklistColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.FileRef) { dataGridField = new GridFileRefColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Integer || column.FieldType == RecordFieldType.BigInt) { dataGridField = new GridIntColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Date) { dataGridField = new GridDateColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Decimal) { dataGridField = new GridDecimalColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Double) { dataGridField = new GridDoubleColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Money) { dataGridField = new GridMoneyColumn() { Binding = cellBinding }; } else if (column.FieldType == RecordFieldType.Url) { dataGridField = new GridUrlColumn() { Binding = cellBinding }; } else { dataGridField = new GridStringColumn() { Binding = cellBinding }; } dataGridField.Header = column.FieldLabel; dataGridField.Width = new DataGridLength(column.WidthPart, DataGridLengthUnitType.Pixel); var isFormReadonly = gridSectionViewModel.IsReadOnly; var isWriteable = gridSectionViewModel?.RecordService?.GetFieldMetadata(column.FieldName, gridSectionViewModel.RecordType).Createable == true || gridSectionViewModel?.RecordService?.GetFieldMetadata(column.FieldName, gridSectionViewModel.RecordType).Writeable == true; dataGridField.IsReadOnly = isFormReadonly || !isWriteable; var description = gridSectionViewModel?.RecordService?.GetFieldMetadata(column.FieldName, gridSectionViewModel.RecordType).Description; //todo this removes the standard xaml setters including the click to sort //var style = new Style(typeof(DataGridColumnHeader)); //style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, description)); //dataGridField.HeaderStyle = style; dynamicDataGrid.Columns.Add(dataGridField); } var dataGridBinding = new Binding { Path = new PropertyPath("GridRecords"), Mode = BindingMode.TwoWay }; dynamicDataGrid.SetBinding(ItemsControl.ItemsSourceProperty, dataGridBinding); var selectedItemBinding = new Binding { Path = new PropertyPath("SelectedRow"), Mode = BindingMode.TwoWay }; dynamicDataGrid.SetBinding(Selector.SelectedItemProperty, selectedItemBinding); }); }); } }