// These methods are not used, but they are very useful when developing the Design experience #if Development /// <summary> /// Adds a default column for each property in the data source /// </summary> public static void GenerateColumns(ModelItem dataGridModelItem, EditingContext context) { using (ModelEditingScope scope = dataGridModelItem.BeginEdit(Properties.Resources.Generate_Columns)) { // Set databinding related properties DataGridDesignHelper.SparseSetValue(dataGridModelItem.Properties[MyPlatformTypes.DataGrid.AutoGenerateColumnsProperty], false); // Get the datasource object dataSource = dataGridModelItem.Properties[MyPlatformTypes.DataGrid.ItemsSourceProperty].ComputedValue; if (dataSource != null) { foreach (ColumnInfo columnGenerationInfo in DataGridDesignHelper.GetColumnGenerationInfos(dataSource)) { ModelItem dataGridColumn = null; dataGridColumn = DataGridDesignHelper.CreateDefaultDataGridColumn(context, columnGenerationInfo); if (dataGridColumn != null) { dataGridModelItem.Properties[MyPlatformTypes.DataGrid.ColumnsProperty].Collection.Add(dataGridColumn); } } } scope.Complete(); } }
private static void OnColumnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PropertyColumnDataModel columnModel = (PropertyColumnDataModel)d; if (columnModel.Column != null) { DataGridDesignHelper.SparseSetValue(columnModel.Column.Properties[PlatformTypes.DataGridColumn.IsReadOnlyProperty], e.NewValue); } }
// Set any property defaults here public override void InitializeDefaults(ModelItem item) { if (item != null) { DataGridDesignHelper.SparseSetValue(item.Properties[PlatformTypes.DataGrid.WidthProperty], 200.0); DataGridDesignHelper.SparseSetValue(item.Properties[PlatformTypes.DataGrid.HeightProperty], 200.0); DataGridDesignHelper.SparseSetValue(item.Properties[PlatformTypes.DataGrid.AutoGenerateColumnsProperty], false); } }
/// <summary> /// When the column changes we need to sync other properties to keep our databinding in sync. /// </summary> private void SyncColumnProperties() { if (!_isSyncing) { _isSyncing = true; try { if (Column == null) { BindingPath = string.Empty; ColumnTypeName = string.Empty; HasColumn = false; } else { SetBindingPath(); ColumnTypeName = DataGridDesignHelper.GetColumnStringType(Column); HasColumn = true; // We sometimes want to pull the properties from the column, and other times we want to // push the models properties down to the column. if (_restoreColumn) { DataGridDesignHelper.SparseSetValue(Column.Properties[PlatformTypes.DataGridColumn.HeaderProperty], ColumnHeader); DataGridDesignHelper.SparseSetValue(Column.Properties[PlatformTypes.DataGridColumn.CanUserReorderProperty], ColumnCanUserReorder); DataGridDesignHelper.SparseSetValue(Column.Properties[PlatformTypes.DataGridColumn.CanUserResizeProperty], ColumnCanUserResize); DataGridDesignHelper.SparseSetValue(Column.Properties[PlatformTypes.DataGridColumn.CanUserSortProperty], ColumnCanUserSort); DataGridDesignHelper.SparseSetValue(Column.Properties[PlatformTypes.DataGridColumn.IsReadOnlyProperty], ColumnIsReadOnly); } else { // Update based on what's set in the ModelItem ModelProperty modelProperty = Column.Properties[PlatformTypes.DataGridColumn.HeaderProperty]; ColumnHeader = modelProperty.IsSet ? (string)modelProperty.ComputedValue : null; modelProperty = Column.Properties[PlatformTypes.DataGridColumn.CanUserReorderProperty]; ColumnCanUserReorder = modelProperty.IsSet ? (bool?)modelProperty.ComputedValue : null; modelProperty = Column.Properties[PlatformTypes.DataGridColumn.CanUserResizeProperty]; ColumnCanUserResize = modelProperty.IsSet ? (bool?)modelProperty.ComputedValue : null; modelProperty = Column.Properties[PlatformTypes.DataGridColumn.CanUserSortProperty]; ColumnCanUserSort = modelProperty.IsSet ? (bool?)modelProperty.ComputedValue : null; modelProperty = Column.Properties[PlatformTypes.DataGridColumn.IsReadOnlyProperty]; ColumnIsReadOnly = modelProperty.IsSet ? (bool?)modelProperty.ComputedValue : null; } } } finally { _isSyncing = false; } } }
/// <summary> /// Fills this collection from a DataGrid. Creates one item for each Column, then one for each property. /// </summary> public PropertyColumnDataModelCollection(PropertyColumnEditor propertyColumnEditor, ModelItem dataGrid) { PropertyColumnEditor = propertyColumnEditor; _dataGrid = dataGrid; _initializing = true; try { object dataSource = _dataGrid.Properties[PlatformTypes.DataGrid.ItemsSourceProperty].ComputedValue; if (dataSource != null) { // Create a PropertyColumnDataModel for each column in the DataGrid's Columns Collection foreach (ModelItem dataGridColumn in _dataGrid.Properties[PlatformTypes.DataGrid.ColumnsProperty].Collection) { PropertyColumnDataModel columnModel = new PropertyColumnDataModel(this); columnModel.SetInitialColumn(dataGridColumn); this.Add(columnModel); } // Look through the column info the DataSource's properties and see if any match existing columns foreach (ColumnInfo columnGenerationInfo in DataGridDesignHelper.GetColumnGenerationInfos(dataSource)) { bool found = false; // only create a new item if we dont already have an item that matches based on the column binding. foreach (PropertyColumnDataModel columnModel in this) { if (columnModel.ColumnGenerationInfo == null && columnModel.BindingPath == columnGenerationInfo.PropertyInfo.Name) { // if we already have a column bound to this property, then must associate the property descriptor with it. columnModel.ColumnGenerationInfo = columnGenerationInfo; found = true; break; } } // if we dont have a column that binds to this property, add a new data object to represent this potential column if (!found) { this.Add(new PropertyColumnDataModel(this) { ColumnGenerationInfo = columnGenerationInfo }); } } } } finally { _initializing = false; } }
private static void OnColumnHeaderPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PropertyColumnDataModel columnModel = (PropertyColumnDataModel)d; if (columnModel.Column != null) { if (e.NewValue == null) { columnModel.Column.Properties[PlatformTypes.DataGridColumn.HeaderProperty].ClearValue(); } else { DataGridDesignHelper.SparseSetValue(columnModel.Column.Properties[PlatformTypes.DataGridColumn.HeaderProperty], e.NewValue); } } }
private static void OnColumnTypeNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { PropertyColumnDataModel columnModel = (PropertyColumnDataModel)d; if (!columnModel._isSyncing && columnModel.Column != null && columnModel._columnGenerationInfo != null && columnModel._columnGenerationInfo.PropertyInfo != null && !string.IsNullOrEmpty(columnModel.ColumnTypeName)) { TypeIdentifier columnTypeId = DataGridDesignHelper.GetColumnSystemType(columnModel.ColumnTypeName); // Update the column with the model properties. If someone changed the header, // we want to keep this value when they change the type. columnModel._restoreColumn = true; columnModel.Column = DataGridDesignHelper.CreateColumnFromColumnType( columnModel._parentCollection.PropertyColumnEditor.EditingContext, columnTypeId, columnModel._columnGenerationInfo); } }
private void CreateColumn() { // The user may have already had a column for this property. If so we want to restore it so any changes // they made will be restored. if (_oldColumnModelItem != null) { // bring back the properties & column that we had before. // pull the model properties from the column _restoreColumn = false; Column = _oldColumnModelItem; } else { // Update the column with the model properties. _restoreColumn = true; ModelItem column = DataGridDesignHelper.CreateDefaultDataGridColumn(_parentCollection.PropertyColumnEditor.EditingContext, _columnGenerationInfo); if (_columnGenerationInfo.HeaderFromAttribute == null) { this.ColumnHeader = _columnGenerationInfo.PropertyInfo.Name; } Column = column; } }
internal void OnHasColumnChanged() { EnsureSelectAllChecked(); DataGridDesignHelper.SparseSetValue(_dataGrid.Properties[PlatformTypes.DataGrid.AutoGenerateColumnsProperty], false); }