Beispiel #1
0
        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)
                {
                    displayMemberBinding = column.GetDisplayMemberBinding();

                    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;
                        }

                        column.IsBindingAutoCreated = true;
                        column.SetDisplayMemberBinding(displayMemberBinding);
                    }
                }

                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);
                    }
                }
            }
        }
Beispiel #2
0
        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();
            }
        }