/// <summary> /// Create DataGridColumns for all the datasoure properties that don't already have columns /// </summary> private void CreateAllButton_Click(object sender, RoutedEventArgs e) { bool hasColumnsAlready = _columns.Count != 0; DataGridColumnTypeModel columnTypeModel = columnTypeComboBox.SelectedItem as DataGridColumnTypeModel; if (columnTypeModel == null) { throw new Exception("unexpected state"); } // Set databinding related properties DataGridHelper.SparseSetValue(_dataGrid.Properties[DataGrid.AutoGenerateColumnsProperty], false); foreach (DataSourcePropertyModel pm in _dataSourceProperties) { bool addThisColumn = true; if (!hasColumnsAlready) { addThisColumn = true; } else { addThisColumn = !(_columns.Includes(dataGridColumnModel => pm.Name.Equals(dataGridColumnModel.BindingPropertyName))); } if (addThisColumn) { ModelItem dataGridColumn = columnTypeModel.CreateDataGridColumn(_context, pm.Property); _columns.Add(dataGridColumn); } } _dataSourcePropertiesCVS.View.Refresh(); }
/// <summary> /// Create a DataGridColumn for the selected data source property /// </summary> private void AddButton_Click(object sender, RoutedEventArgs e) { DataGridColumnTypeModel columnTypeModel = columnTypeComboBox.SelectedItem as DataGridColumnTypeModel; if (columnTypeModel == null) { throw new Exception("unexpected state"); } if (dataSourceColumnsListBox.SelectedItems.Count > 0) { int oldSelectedIndex = dataSourceColumnsListBox.SelectedIndex; DataSourcePropertyModel[] itemsToAdd = new DataSourcePropertyModel[dataSourceColumnsListBox.SelectedItems.Count]; dataSourceColumnsListBox.SelectedItems.CopyTo(itemsToAdd, 0); foreach (DataSourcePropertyModel pm in itemsToAdd) { ModelItem dataGridColumn = columnTypeModel.CreateDataGridColumn(_context, pm.Property); _columns.Add(dataGridColumn); } DataGridHelper.SparseSetValue(_dataGrid.Properties[DataGrid.AutoGenerateColumnsProperty], false); _dataSourcePropertiesCVS.View.Refresh(); dataSourceColumnsListBox.SelectedIndex = Math.Min(oldSelectedIndex, dataSourceColumnsListBox.Items.Count - 1); } }
/// <summary> /// Add Columns based on the datasource /// </summary> private void AddColumns(ModelItem selectedDataGrid, EditingContext context) { using (ModelEditingScope scope = selectedDataGrid.BeginEdit("Generate Columns")) { // Set databinding related properties DataGridHelper.SparseSetValue(selectedDataGrid.Properties[DataGrid.AutoGenerateColumnsProperty], false); // Get the datasource object dataSource = selectedDataGrid.Properties[ItemsControl.ItemsSourceProperty].ComputedValue; if (dataSource != null) { // Does WPF expose something like ListBindingHelper? PropertyDescriptorCollection dataSourceProperties = System.Windows.Forms.ListBindingHelper.GetListItemProperties(dataSource); foreach (PropertyDescriptor pd in dataSourceProperties) { ModelItem dataGridColumn = null; dataGridColumn = DataGridHelper.CreateDefaultDataGridColumn(context, pd); if (dataGridColumn != null) { selectedDataGrid.Properties["Columns"].Collection.Add(dataGridColumn); } } } scope.Complete(); } }
// Set any property defaults here public override void InitializeDefaults(ModelItem item) { if (item != null) { DataGridHelper.SparseSetValue(item.Properties["Width"], 180.0); DataGridHelper.SparseSetValue(item.Properties["Height"], 170.0); } }
// Set any property defaults here public override void InitializeDefaults(ModelItem item) { if (item != null) { DataGridHelper.SparseSetValue(item.Properties["Width"], 200.0); DataGridHelper.SparseSetValue(item.Properties["Height"], 200.0); DataGridHelper.SparseSetValue(item.Properties["AutoGenerateColumns"], false); } }
// Set any property defaults here public override void InitializeDefaults(ModelItem item) { if (item != null) { // This will really cause a margin to be added. Size defaultsize = new Size(200, 200); DataGridHelper.SparseSetValue(item.Properties[FrameworkElement.WidthProperty], defaultsize.Width); DataGridHelper.SparseSetValue(item.Properties[FrameworkElement.HeightProperty], defaultsize.Height); } }
private void CreateUnboundButton_Click(object sender, RoutedEventArgs e) { DataGridColumnTypeModel columnTypeModel = columnTypeComboBox.SelectedItem as DataGridColumnTypeModel; if (columnTypeModel == null) { throw new Exception("unexpected state"); } // Set databinding related properties DataGridHelper.SparseSetValue(_dataGrid.Properties[DataGrid.AutoGenerateColumnsProperty], false); ModelItem dataGridColumn = columnTypeModel.CreateDataGridColumn(_context); _columns.Add(dataGridColumn); }