protected internal virtual void SetupDisplayMemberBinding(DataGridContext dataGridContext) { // Bind the cell content. Column column = this.ParentColumn as Column; if (column != null) { BindingBase displayMemberBinding = null; object dataContext = this.ParentRow.DataContext; // If the dataContext is our ParentRow, we do not create any binding if (dataContext != this.ParentRow) { // Disable warning for DisplayMemberBinding when internaly used #pragma warning disable 618 displayMemberBinding = column.DisplayMemberBinding; #pragma warning restore 618 if (displayMemberBinding == null) { if ((dataGridContext == null) || (dataGridContext.ItemsSourceFieldDescriptors == null)) { throw new InvalidOperationException("An attempt was made to create a DisplayMemberBinding before the DataGridContext has been initialized."); } if (!DesignerProperties.GetIsInDesignMode(this)) { bool isDataGridUnboundItemProperty; displayMemberBinding = ItemsSourceHelper.AutoCreateDisplayMemberBinding( column, dataGridContext, dataContext, out isDataGridUnboundItemProperty); column.IsBoundToDataGridUnboundItemProperty = isDataGridUnboundItemProperty; } // Disable warning for DisplayMemberBinding when internaly used #pragma warning disable 618 column.DisplayMemberBinding = displayMemberBinding; #pragma warning restore 618 //mark the Column's Binding as AutoCreated. column.IsBindingAutoCreated = true; } } if (displayMemberBinding != null) { m_canBeRecycled = DataCell.VerifyDisplayMemberBinding(displayMemberBinding); BindingOperations.SetBinding(this, Cell.ContentProperty, displayMemberBinding); XmlElement xmlElement = this.GetValue(Cell.ContentProperty) as XmlElement; if (xmlElement != null) { // Convert binding to an InnerXML binding in the case we are bound on a XmlElement // to be able to refresh the data in the XML. //under any circumstances, a cell that is bound to XML cannot be recycled m_canBeRecycled = false; BindingOperations.ClearBinding(this, Cell.ContentProperty); this.ClearValue(Cell.ContentProperty); Binding xmlElementBinding = new Binding("InnerXml"); xmlElementBinding.Source = xmlElement; xmlElementBinding.Mode = BindingMode.TwoWay; xmlElementBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit; BindingOperations.SetBinding(this, Cell.ContentProperty, xmlElementBinding); } } } }
/// <summary> /// Gets current background brush for cell. /// </summary> /// <param name="cell">Cell.</param> /// <returns>Current background brush.</returns> private Brush _GetBackgroundBrushForCell(DataCell cell) { Debug.Assert(cell != null); // Get layoutroot to get access to background brushes. Grid layoutRootGrid = _GetLayoutRootGrid(cell); // Get datagridcontrol to check is location first in collection. DataGridControl dataGridControl = XceedVisualTreeHelper.FindParent<DataGridControl>(cell); int indexOfItem = -1; try { // Workaround: Sometimes grid throws exception. indexOfItem = dataGridControl.Items.IndexOf(cell.DataContext); } catch { } Brush backgroundBrush; if (indexOfItem == 0) { // Use "Add location" string for first item. backgroundBrush = (Brush)layoutRootGrid.Resources[AddLocationBrushResourceName]; } else { // Use "Add another location" string for other items. backgroundBrush = (Brush)layoutRootGrid.Resources[AddAnotherLocationBrushResourceName]; } return backgroundBrush; }
/// <summary> /// Walk through visual tree and get Layout root grid. /// </summary> /// <param name="cell">Initial cell.</param> /// <returns>Layout root grid.</returns> private Grid _GetLayoutRootGrid(DataCell cell) { Debug.Assert(cell != null); Grid result = null; FrameworkElement frameworkElement = cell as FrameworkElement; while (frameworkElement != null) { object parent = VisualTreeHelper.GetParent(frameworkElement); // Get parent. if (parent != null) { frameworkElement = parent as FrameworkElement; } else { frameworkElement = frameworkElement.Parent as FrameworkElement; } // Check grid found. Grid grid = frameworkElement as Grid; if (grid != null && grid.Name.Equals(LayoutRootGridName, StringComparison.OrdinalIgnoreCase)) { result = grid; break; } } return result; }
protected internal virtual void SetupDisplayMemberBinding(DataGridContext dataGridContext) { // Bind the cell content. var column = this.ParentColumn as Column; if (column != null) { var displayMemberBinding = default(BindingBase); var dataItem = this.ParentRow.DataContext; // If the dataContext is our ParentRow, we do not create any binding if (dataItem != this.ParentRow) { displayMemberBinding = column.GetDisplayMemberBinding(); if (displayMemberBinding == null) { if (dataGridContext == null) { throw new InvalidOperationException("An attempt was made to create a DisplayMemberBinding before the DataGridContext has been initialized."); } if (!DesignerProperties.GetIsInDesignMode(this)) { var propertyDescription = ItemsSourceHelper.CreateOrGetPropertyDescriptionFromColumn(dataGridContext, column, (dataItem != null) ? dataItem.GetType() : null); ItemsSourceHelper.UpdateColumnFromPropertyDescription(column, dataGridContext.DataGridControl.DefaultCellEditors, dataGridContext.AutoCreateForeignKeyConfigurations, propertyDescription); displayMemberBinding = column.GetDisplayMemberBinding(); } column.IsBindingAutoCreated = true; } } if (displayMemberBinding != null) { m_canBeRecycled = DataCell.VerifyDisplayMemberBinding(displayMemberBinding); BindingOperations.SetBinding(this, Cell.ContentProperty, displayMemberBinding); var xmlElement = this.GetValue(Cell.ContentProperty) as XmlElement; if (xmlElement != null) { // Convert binding to an InnerXML binding in the case we are bound on a XmlElement // to be able to refresh the data in the XML. //under any circumstances, a cell that is bound to XML cannot be recycled m_canBeRecycled = false; this.ClearDisplayMemberBinding(); var xmlElementBinding = new Binding("InnerXml"); xmlElementBinding.Source = xmlElement; xmlElementBinding.Mode = BindingMode.TwoWay; xmlElementBinding.UpdateSourceTrigger = UpdateSourceTrigger.Explicit; BindingOperations.SetBinding(this, Cell.ContentProperty, xmlElementBinding); } } else { this.ClearDisplayMemberBinding(); } } else { this.ClearDisplayMemberBinding(); } }