/// <summary> /// Handles changes to the Matrix property. /// </summary> /// <param name="d">PivotItem</param> /// <param name="e">DependencyProperty changed event arguments</param> private static void OnMatrixChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var mextrix = e.NewValue as IMatrix; var dataGridCollectionControl = d as DataGridCollectionControl; if ((mextrix == null) || (dataGridCollectionControl == null)) { return; } for (var i = 0; i < mextrix.ColumnCount; i++) { var propertyPathName = "Item[" + i + "]"; var bind = new Binding { Path = new PropertyPath(propertyPathName), Mode = BindingMode.OneWay }; var cbc = new CustomBoundColumn { Header = "Sw_" + i, Binding = bind, TemplateName = "CustomTemplate", SortMemberPath = propertyPathName }; dataGridCollectionControl.MyDataGrid.Columns.Add(cbc); //var col = new DataGridTemplateColumn(); //col.Header = "Sw_" + i; //var propertyPathName = "Item[" + i + "]"; //col.SortMemberPath = propertyPathName; ////col.Binding = new Binding(path:"Item["+i+"]"); //dataGridCollectionControl.MyDataGrid.Columns.Add(col); //// First: create and add the data template to the parent control //var dt = new DataTemplate(typeof(TextBox)); //col.CellTemplate = dt; //// Second: create and add the text box to the data template //var txtElement = new FrameworkElementFactory(typeof(TextBox)); //dt.VisualTree = txtElement; //// Create binding //var bind = new Binding(); //bind.Path = new PropertyPath(propertyPathName); //bind.Mode = BindingMode.TwoWay; //// Third: set the binding in the text box //txtElement.SetBinding(TextBox.TextProperty, bind); //txtElement.SetValue(FontSizeProperty, 18.0); } }
private static void AddColumns(DataGrid dataGrid, IEnumerable columns) { foreach (var column in columns) { CustomBoundColumn customBoundColumn = new CustomBoundColumn() { Header = column, HeaderTemplate = GetHeaderTemplate(dataGrid), CellTemplate = GetAttachedCellTemplate(dataGrid), CellEditingTemplate = GetAttachedCellEditingTemplate(dataGrid), MappedValueCollection = GetMappedValues(dataGrid) }; customBoundColumn.MinWidth = 50; dataGrid.Columns.Add(customBoundColumn); } }
private static void OnMappedValuesPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) { var mappedValueCollection = e.NewValue as MappedValueCollection; var dataGrid = dependencyObject as DataGrid; var existingColumns = new List<CustomBoundColumn>(); foreach (var column in dataGrid.Columns) { if (column is CustomBoundColumn) { existingColumns.Add(column as CustomBoundColumn); } } if (existingColumns.Count > 0) { foreach (var column in existingColumns) { dataGrid.Columns.Remove(column); } foreach (var column in (IEnumerable)dependencyObject.GetValue(AttachedColumnBehavior.AttachedColumnsProperty)) { CustomBoundColumn customBoundColumn = new CustomBoundColumn() { Header = column, HeaderTemplate = GetHeaderTemplate(dataGrid), CellTemplate = GetAttachedCellTemplate(dataGrid), CellEditingTemplate = GetAttachedCellEditingTemplate(dataGrid), MappedValueCollection = GetMappedValues(dataGrid) }; customBoundColumn.MinWidth = 50; dataGrid.Columns.Add(customBoundColumn); } } }