internal override void OnEditCanceled(DataGridItemEventArgs e)
        {
            var item        = e.Item;
            var pageManager = this.RootGroup.GetVirtualPageManager();

            // Compare cached values with current values.  If they are the same, we can clear the old values which in turn will
            // make the item non dirty.
            var clearIsDirty = true;

            var cachedValues = pageManager.GetCachedValuesForItem(item);

            Debug.Assert(cachedValues != null);

            var itemProperties = this.ItemProperties;

            foreach (var itemProperty in itemProperties)
            {
                var currentValue = ItemsSourceHelper.GetValueFromItemProperty(itemProperty, item);

                if (!(object.Equals(currentValue, cachedValues[PropertyRouteParser.Parse(itemProperty)])))
                {
                    clearIsDirty = false;
                    break;
                }
            }

            if (clearIsDirty)
            {
                pageManager.ClearCachedValuesForItem(item);
            }

            base.OnEditCanceled(e);
        }
        internal static bool TryGetDetailColumnName(DataGridItemPropertyMap itemPropertyMap, string masterColumnName, out string detailColumnName)
        {
            detailColumnName = default(string);

            if (itemPropertyMap == null)
            {
                return(false);
            }

            var masterItemProperty = ItemsSourceHelper.GetItemPropertyFromProperty(itemPropertyMap.MasterItemProperties, masterColumnName);

            if (masterItemProperty == null)
            {
                return(false);
            }

            DataGridItemPropertyBase detailItemProperty;

            if (!itemPropertyMap.TryGetDetailItemProperty(masterItemProperty, out detailItemProperty))
            {
                return(false);
            }

            detailColumnName = PropertyRouteParser.Parse(detailItemProperty);

            return(!string.IsNullOrEmpty(detailColumnName));
        }
        internal override void OnEditBegun(DataGridItemEventArgs e)
        {
            var item        = e.Item;
            var pageManager = this.RootGroup.GetVirtualPageManager();

            if (!pageManager.IsItemDirty(item))
            {
                // First time we enter edit on this item.
                var itemProperties = this.ItemProperties;
                var count          = itemProperties.Count;

                var propertyNames = new string[count];
                var cachedValues  = new object[count];

                for (int i = 0; i < count; i++)
                {
                    var itemProperty = itemProperties[i];

                    propertyNames[i] = PropertyRouteParser.Parse(itemProperty);
                    cachedValues[i]  = ItemsSourceHelper.GetValueFromItemProperty(itemProperty, item);
                }

                // Cache the values of the never edited before row.  This will help the developer find the corresponding row
                // in the source when times comes to commit the changes to the data source.
                pageManager.SetCachedValuesForItem(item, propertyNames, cachedValues);
            }

            base.OnEditBegun(e);
        }
        protected internal override IEnumerable GetDetailsForParentItem(DataGridCollectionViewBase parentCollectionView, object parentItem)
        {
            EntityObject entityObject = parentItem as EntityObject;

            if (entityObject == null)
            {
                return(null);
            }

            // Even if EntityObject is not in a loadable state, we must still return the IList
            // so that the ItemProperties can be extracted based on the elements type.
            bool entityObjectLoadable = ItemsSourceHelper.IsEntityObjectLoadable(entityObject);

            // We let the user take charge of handling the details.
            QueryEntityDetailsEventArgs args = new QueryEntityDetailsEventArgs(entityObject);

            if (entityObjectLoadable)
            {
                this.OnQueryDetails(args);
            }

            // The parentItem must implement IEntityWithRelationships
            Type parentItemType = parentItem.GetType();

            if (typeof(IEntityWithRelationships).IsAssignableFrom(parentItemType))
            {
                // Since the relationship was based on the the property
                // name, we must find that property using reflection.
                PropertyInfo propertyInfo = parentItemType.GetProperty(this.RelationName);

                if (propertyInfo != null)
                {
                    RelatedEnd relatedEnd = propertyInfo.GetValue(parentItem, null) as RelatedEnd;

                    if (relatedEnd != null)
                    {
                        // Make sure that the details are loaded
                        // except if the user already handled it.
                        if (!relatedEnd.IsLoaded &&
                            !args.Handled &&
                            entityObjectLoadable)
                        {
                            relatedEnd.Load();
                        }

                        IListSource listSource = relatedEnd as IListSource;

                        // Returns an IList to have proper change notification events.
                        if (listSource != null)
                        {
                            return(listSource.GetList());
                        }
                    }
                }
            }

            return(null);
        }
        protected object GetPropertyValue(object item)
        {
            if (m_contextProperty != null)
            {
                return(ItemsSourceHelper.GetValueFromItemProperty(m_contextProperty, item));
            }

            return(m_propertyGroupDescription.GroupNameFromItem(item, 0, CultureInfo.InvariantCulture));
        }
        protected override void RestoreStateCore(IDataGridContextVisitable dataGridContextVisitable)
        {
            try
            {
                m_rootDataGridContext = SaveRestoreDataGridContextStateVisitor.GetDataGridContextFromVisitable(dataGridContextVisitable);

                if (m_saveExpandedItems)
                {
                    for (int i = 0; i < m_itemsToExpand.Count; i++)
                    {
                        object dataItemToExpand = m_itemsToExpand[i].Target;

                        if (dataItemToExpand != null)
                        {
                            try
                            {
                                // Verify if we have a System.Data.DataView as ItemsSource to
                                // ensure to restore the System.Data.DataRowView that represents
                                // the System.Data.DataRow saved previously
                                System.Data.DataView dataView =
                                    ItemsSourceHelper.TryGetDataViewFromDataGridContext(m_rootDataGridContext);

                                if ((dataView != null) &&
                                    (dataItemToExpand is System.Data.DataRow))
                                {
                                    foreach (System.Data.DataRowView dataRowView in dataView)
                                    {
                                        if (dataRowView.Row == dataItemToExpand)
                                        {
                                            dataItemToExpand = dataRowView;
                                            break;
                                        }
                                    }
                                }

                                m_rootDataGridContext.ExpandDetails(dataItemToExpand);
                            }
                            catch
                            {
                                System.Diagnostics.Debug.Assert(false, "Item has moved in the source ?");
                            }
                        }
                    }
                }

                bool visitWasStopped;
                dataGridContextVisitable.AcceptVisitor(0, int.MaxValue, this, DataGridContextVisitorType.Groups, false, out visitWasStopped);
            }
            finally
            {
                m_itemsToExpand       = null;
                m_rootDataGridContext = null;
            }
        }
        protected override void SavingVisit(DataGridContext sourceContext)
        {
            if (sourceContext.ParentDataGridContext == m_rootDataGridContext)
            {
                // Ensure to get a reference to the System.Data.DataRow when doing a
                // save/restore of a System.Data.DataRowView since the view is recreated
                // for every detail views
                object parentItem = ItemsSourceHelper.TryGetDataRowFromDataItem(sourceContext.ParentItem);

                m_itemsToExpand.Add(new WeakReference(parentItem));
            }
        }
Ejemplo n.º 8
0
        protected virtual void SetTitleBarContentBinding(DataGridContext dataGridContext)
        {
            if (dataGridContext != null)
            {
                Xceed.Wpf.DataGrid.Views.ViewBase view = dataGridContext.DataGridControl.GetView();

                if ((view is TableView) ||
                    (view is TableflowView))
                {
                    return;
                }

                Column headerColumn = dataGridContext.Columns.MainColumn as Column;

                if (headerColumn != null)
                {
                    // Disable warning for DisplayMemberBinding when internaly used
#pragma warning disable 618

                    BindingBase displayMemberBinding = headerColumn.DisplayMemberBinding;

#pragma warning restore 618

                    if (displayMemberBinding == null)
                    {
                        if (dataGridContext.ItemsSourceFieldDescriptors == null)
                        {
                            throw new InvalidOperationException("An attempt was made to create a DisplayMemberBinding before the DataGridContext has been initialized.");
                        }

                        string name = headerColumn.FieldName;
                        ItemsSourceHelper.FieldDescriptor fieldDescriptor;
                        dataGridContext.ItemsSourceFieldDescriptors.TryGetValue(name, out fieldDescriptor);

                        displayMemberBinding = ItemsSourceHelper.CreateDefaultBinding(
                            this.DataContext is DataRow, name, fieldDescriptor,
                            false, true, typeof(object));
                    }

                    if (displayMemberBinding == null)
                    {
                        Debug.Assert(false, "displayMemberBinding is null.");
                        this.ClearValue(DataRow.TitleBarContentProperty);
                    }
                    else
                    {
                        this.SetBinding(DataRow.TitleBarContentProperty, displayMemberBinding);
                    }
                }
            }
        }
        public DataRowColumnPropertyDescriptor(System.Data.DataColumn column)
            : base(column.ColumnName, null)
        {
            if (column == null)
            {
                throw new ArgumentNullException("column");
            }

            m_columnName     = column.ColumnName;
            m_columnDataType = ItemsSourceHelper.GetColumnDataType(column);
            m_columnCaption  = column.Caption;
            m_columnReadOnly = column.ReadOnly;
            m_columnHidden   = (column.ColumnMapping == MappingType.Hidden);
        }
        private bool TryGetRecycledCell(ColumnBase column, out Cell cell)
        {
            cell = null;

            if (m_recyclingBins == null)
            {
                return(false);
            }

            // Make sure the cell recycling group is up-to-date.
            var dataGridContext = this.DataGridContext;

            if (dataGridContext != null)
            {
                var propertyDescription = ItemsSourceHelper.CreateOrGetPropertyDescriptionFromColumn(dataGridContext, column, null);
                ItemsSourceHelper.UpdateColumnFromPropertyDescription(column, dataGridContext.DataGridControl.DefaultCellEditors, dataGridContext.AutoCreateForeignKeyConfigurations, propertyDescription);
            }

            var recyclingGroup = this.GetRecyclingGroup(column);
            var recycleBin     = default(LinkedList <Cell>);

            if (m_recyclingBins.TryGetValue(recyclingGroup, out recycleBin))
            {
                // Try to recycle a cell that has already the appropriate column in order to minimize the cell's initialization time.
                var targetNode = default(LinkedListNode <Cell>);

                for (var node = recycleBin.Last; node != null; node = node.Previous)
                {
                    targetNode = node;

                    if (targetNode.Value.ParentColumn == column)
                    {
                        break;
                    }
                }

                // From here, the target node is either:
                //   1. The cell with minimal initialization time.
                //   2. The oldest cell.
                //   3. null in case of an empty bin.
                if (targetNode != null)
                {
                    cell = targetNode.Value;
                    recycleBin.Remove(targetNode);
                }
            }

            return(cell != null);
        }
        protected internal override void Initialize(DataGridCollectionViewBase parentCollectionView)
        {
            base.Initialize(parentCollectionView);

            if (this.PropertyDescriptor != null)
            {
                return;
            }

            string relationName = this.RelationName;

            if (String.IsNullOrEmpty(relationName) == true)
            {
                throw new InvalidOperationException("An attempt was made to initialize a PropertyDetailDescription whose Name property has not been set.");
            }

            var enumeration = parentCollectionView as IEnumerable;

            if (enumeration != null)
            {
                // Try to get it from the first item in the DataGridCollectionView
                var firstItem = ItemsSourceHelper.GetFirstItemByEnumerable(enumeration);
                if (firstItem != null)
                {
                    var propertyDescriptor = this.GetPropertyDescriptorFromFirstItem(firstItem);
                    if (propertyDescriptor != null)
                    {
                        this.PropertyDescriptor = propertyDescriptor;
                        return;
                    }
                }
            }

            // If the list is empty, check if the SourceCollection is ITypedList
            var typedList = parentCollectionView.SourceCollection as ITypedList;

            if (typedList != null)
            {
                var propertyDescriptor = this.GetPropertyDescriptorFromITypedList(typedList);
                if (propertyDescriptor != null)
                {
                    this.PropertyDescriptor = propertyDescriptor;
                    return;
                }
            }

            throw new InvalidOperationException("An attempt was made to initialize a PropertyDetailDescription whose data source does not contain a property that corresponds to the specified relation name.");
        }
        private void OnItemPropertyPropertyChanged(DataGridItemPropertyBase itemProperty, PropertyChangedEventArgs e)
        {
            var rootCollection = ItemsSourceHelper.GetRootCollection(itemProperty);

            if (rootCollection == null)
            {
                return;
            }

            using (this.DeferMappingChanged())
            {
                if (string.IsNullOrEmpty(e.PropertyName) || (e.PropertyName == DataGridItemPropertyBase.SynonymPropertyName))
                {
                    if (rootCollection == m_detailItemProperties)
                    {
                        this.MapDetailItemProperty(itemProperty);
                    }
                }

                if (string.IsNullOrEmpty(e.PropertyName) || (e.PropertyName == DataGridItemPropertyBase.ItemPropertiesInternalPropertyName))
                {
                    var itemProperties = itemProperty.ItemPropertiesInternal;
                    if (itemProperties != null)
                    {
                        this.UnregisterItemProperties(itemProperties);
                        this.RegisterItemProperties(itemProperties);

                        if (rootCollection == m_masterItemProperties)
                        {
                            foreach (var childItemProperty in itemProperties)
                            {
                                this.MapMasterItemProperty(childItemProperty);
                            }
                        }
                        else if (rootCollection == m_detailItemProperties)
                        {
                            foreach (var childItemProperty in itemProperties)
                            {
                                this.MapDetailItemProperty(childItemProperty);
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 13
0
        protected virtual void SetTitleBarContentBinding(DataGridContext dataGridContext)
        {
            if (dataGridContext == null)
            {
                return;
            }

            var view = dataGridContext.DataGridControl.GetView();

            if ((view is TableView) || (view is TableflowView))
            {
                return;
            }

            var headerColumn = dataGridContext.Columns.MainColumn as Column;

            if (headerColumn == null)
            {
                return;
            }

            var displayMemberBinding = headerColumn.GetDisplayMemberBinding();

            if (displayMemberBinding == null)
            {
                var dataItem = this.DataContext;
                var itemType = (dataItem != null) ? dataItem.GetType() : null;

                displayMemberBinding = ItemsSourceHelper.CreateDefaultBinding(
                    ItemsSourceHelper.CreateOrGetPropertyDescriptionFromColumn(dataGridContext, headerColumn, itemType));
            }

            if (displayMemberBinding == null)
            {
                Debug.Assert(false, "displayMemberBinding is null.");
                this.ClearValue(DataRow.TitleBarContentProperty);
            }
            else
            {
                this.SetBinding(DataRow.TitleBarContentProperty, displayMemberBinding);
            }
        }
        internal void SetContext(DataGridCollectionView collectionView)
        {
            if (collectionView == null)
            {
                m_contextProperty = null;
            }
            else
            {
                var propertyName = m_propertyGroupDescription.PropertyName;

                if (string.IsNullOrEmpty(propertyName))
                {
                    m_contextProperty = null;
                }
                else
                {
                    m_contextProperty = ItemsSourceHelper.GetItemPropertyFromProperty(collectionView.ItemProperties, propertyName);
                }
            }
        }
        internal IValueConverter GetBindingConverter(object sourceItem)
        {
            if (!this.IsSealed)
            {
                throw new InvalidOperationException("An attempt was made to apply a binding to a DataGridItemProperty that has not be added to the ItemProperties collection.");
            }

            if (m_bindingConverter == null)
            {
                if (m_converter != null)
                {
                    m_bindingConverter = m_converter;
                }
                else
                {
                    m_bindingConverter = new SourceDataConverter(ItemsSourceHelper.IsItemSupportingDBNull(sourceItem), CultureInfo.InvariantCulture);
                }
            }

            return(m_bindingConverter);
        }
        public WeakDataGridContextKey(DataGridContext dataGridContext)
        {
            DataGridContext parentDataGridContext = dataGridContext.ParentDataGridContext;
            int             level = 0;

            while (parentDataGridContext != null)
            {
                level++;
                parentDataGridContext = parentDataGridContext.ParentDataGridContext;
            }

            System.Diagnostics.Debug.Assert(((level == 0) || (dataGridContext.SourceDetailConfiguration != null)),
                                            "A child dataGridContext must have a SourceDetailConfiguration.");

            m_sourceDetailConfigurationRelationName = (dataGridContext.SourceDetailConfiguration != null) ?
                                                      dataGridContext.SourceDetailConfiguration.RelationName : string.Empty;

            if (level > 0)
            {
                // We are NOT dealing with the root DataGridContext.

                // Build the tree of master items.
                m_weakItemsTree = new WeakReference[level];

                DataGridContext tempDataGridContext = dataGridContext;
                for (int i = level - 1; i >= 0; i--)
                {
                    // Ensure to get a reference to the System.Data.DataRow when doing a
                    // save/restore of a System.Data.DataRowView since the view is recreated
                    // for every detail views
                    object parentItem = ItemsSourceHelper.TryGetDataRowFromDataItem(tempDataGridContext.ParentItem);

                    m_weakItemsTree[i] = new WeakReference(parentItem);

                    tempDataGridContext = tempDataGridContext.ParentDataGridContext;
                }
            }

            this.Initialize();
        }
        protected virtual bool OnReceiveWeakEvent(Type managerType, object sender, EventArgs e)
        {
            if ((managerType == null) || (sender == null) || (e == null))
            {
                return(false);
            }

            if (managerType == typeof(CollectionChangedEventManager))
            {
                var eventArgs = ( NotifyCollectionChangedEventArgs )e;

                if (m_itemProperties == sender)
                {
                    this.OnItemPropertiesCollectionChanged(sender, eventArgs);
                }
                else if (m_detailDescriptions == sender)
                {
                }
            }
            else if (managerType == typeof(InitializeItemPropertyEventManager))
            {
                var eventArgs = ( InitializeItemPropertyEventArgs )e;

                if (m_itemProperties == sender)
                {
                    var itemProperty      = eventArgs.ItemProperty;
                    var itemPropertyRoute = DataGridItemPropertyRoute.Create(itemProperty);

                    ItemsSourceHelper.SetPropertyDescriptionsFromItemProperty(m_defaultPropertyDescriptions, null, null, m_itemType, itemPropertyRoute);
                    ItemsSourceHelper.InitializePropertyDescriptions(m_defaultPropertyDescriptions, itemPropertyRoute, m_itemType, this.DefaultPropertyDescriptionsCreated);
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
        internal WeakDataGridContextKey(DataGridContext dataGridContext)
        {
            var parentDataGridContext = dataGridContext.ParentDataGridContext;
            var level = 0;

            while (parentDataGridContext != null)
            {
                level++;
                parentDataGridContext = parentDataGridContext.ParentDataGridContext;
            }

            Debug.Assert(((level == 0) || (dataGridContext.SourceDetailConfiguration != null)), "A child dataGridContext must have a SourceDetailConfiguration.");

            m_sourceDetailConfigurationRelationName = (dataGridContext.SourceDetailConfiguration != null) ? dataGridContext.SourceDetailConfiguration.RelationName : string.Empty;

            if (level > 0)
            {
                // We are NOT dealing with the root DataGridContext.

                // Build the tree of master items.
                m_weakItemsTree = new WeakReference[level];

                var tempDataGridContext = dataGridContext;
                for (int i = level - 1; i >= 0; i--)
                {
                    // Ensure to get a reference to the System.Data.DataRow when doing a
                    // save/restore of a System.Data.DataRowView since the view is recreated
                    // for every detail views
                    var parentItem = ItemsSourceHelper.TryGetDataRowFromDataItem(tempDataGridContext.ParentItem);

                    m_weakItemsTree[i] = new WeakReference(parentItem);

                    tempDataGridContext = tempDataGridContext.ParentDataGridContext;
                }
            }

            m_hashCode = WeakDataGridContextKey.CalculateHashCode(m_sourceDetailConfigurationRelationName, m_weakItemsTree);
        }
Ejemplo n.º 19
0
        private PropertyDescriptor GetPropertyDescriptorFromFirstItem(object firstItem)
        {
            if (string.IsNullOrEmpty(this.RelationName) || (firstItem == null))
            {
                return(null);
            }

            var descriptor = ItemsSourceHelper.GetCustomTypeDescriptor(firstItem, firstItem.GetType());

            if (descriptor == null)
            {
                return(null);
            }

            var properties = descriptor.GetProperties();

            if ((properties != null) && (properties.Count > 0))
            {
                return(properties[this.RelationName]);
            }

            return(null);
        }
 public RemovableItemsItemsControl()
 {
     itemsSourceHelper = new ItemsSourceHelper();
 }
        public int Compare(RawItem xRawItem, RawItem yRawItem)
        {
            if (xRawItem == null)
            {
                return((yRawItem == null) ? 0 : -1);
            }

            if (yRawItem == null)
            {
                return(1);
            }

            var lastSortDirection    = ListSortDirection.Ascending;
            var sortDescriptions     = m_collectionView.SortDescriptions;
            var sortDescriptionCount = sortDescriptions.Count;

            if (sortDescriptionCount > 0)
            {
                var result         = default(int);
                var itemProperties = m_collectionView.ItemProperties;

                for (int i = 0; i < sortDescriptionCount; i++)
                {
                    var sortDescription = sortDescriptions[i];
                    lastSortDirection = sortDescription.Direction;

                    var itemProperty = ItemsSourceHelper.GetItemPropertyFromProperty(itemProperties, sortDescription.PropertyName);
                    if (itemProperty == null)
                    {
                        continue;
                    }

                    var supportInitialize = itemProperty as ISupportInitialize;
                    var xData             = default(object);
                    var yData             = default(object);

                    if (supportInitialize != null)
                    {
                        supportInitialize.BeginInit();
                    }

                    try
                    {
                        xData = ItemsSourceHelper.GetValueFromItemProperty(itemProperty, xRawItem.DataItem);
                        yData = ItemsSourceHelper.GetValueFromItemProperty(itemProperty, yRawItem.DataItem);

                        if (itemProperty.IsSortingOnForeignKeyDescription)
                        {
                            var foreignKeyDescription = itemProperty.ForeignKeyDescription;

                            xData = foreignKeyDescription.GetDisplayValue(xData);
                            yData = foreignKeyDescription.GetDisplayValue(yData);
                        }
                    }
                    finally
                    {
                        if (supportInitialize != null)
                        {
                            supportInitialize.EndInit();
                        }
                    }

                    var sortComparer = itemProperty.SortComparer;

                    if (sortComparer != null)
                    {
                        result = sortComparer.Compare(xData, yData);
                    }
                    else
                    {
                        result = ObjectDataStore.CompareData(xData, yData);
                    }

                    if (result != 0)
                    {
                        if (lastSortDirection == ListSortDirection.Descending)
                        {
                            return(-result);
                        }

                        return(result);
                    }
                }
            }

            if (lastSortDirection == ListSortDirection.Descending)
            {
                return(yRawItem.Index - xRawItem.Index);
            }

            return(xRawItem.Index - yRawItem.Index);
        }
Ejemplo n.º 22
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();
            }
        }
        private void OnItemPropertyCollectionChanged(DataGridItemPropertyCollection collection, NotifyCollectionChangedEventArgs e)
        {
            var rootCollection = ItemsSourceHelper.GetRootCollection(collection);

            if (rootCollection == null)
            {
                return;
            }

            if (rootCollection == m_masterItemProperties)
            {
                if (e.Action == NotifyCollectionChangedAction.Reset)
                {
                    throw new NotSupportedException();
                }

                if (e.Action == NotifyCollectionChangedAction.Move)
                {
                    return;
                }

                using (this.DeferMappingChanged())
                {
                    if (e.OldItems != null)
                    {
                        foreach (DataGridItemPropertyBase itemProperty in e.OldItems)
                        {
                            this.UnregisterItemProperty(itemProperty);
                            this.UnmapMasterItemProperty(itemProperty);
                        }
                    }

                    if (e.NewItems != null)
                    {
                        foreach (DataGridItemPropertyBase itemProperty in e.NewItems)
                        {
                            this.RegisterItemProperty(itemProperty);
                            this.MapMasterItemProperty(itemProperty);
                        }
                    }
                }
            }
            else if (rootCollection == m_detailItemProperties)
            {
                if (e.Action == NotifyCollectionChangedAction.Reset)
                {
                    throw new NotSupportedException();
                }

                if (e.Action == NotifyCollectionChangedAction.Move)
                {
                    return;
                }

                using (this.DeferMappingChanged())
                {
                    if (e.OldItems != null)
                    {
                        foreach (DataGridItemPropertyBase itemProperty in e.OldItems)
                        {
                            this.UnregisterItemProperty(itemProperty);
                            this.UnmapDetailItemProperty(itemProperty);
                        }
                    }

                    if (e.NewItems != null)
                    {
                        foreach (DataGridItemPropertyBase itemProperty in e.NewItems)
                        {
                            this.RegisterItemProperty(itemProperty);
                            this.MapDetailItemProperty(itemProperty);
                        }
                    }
                }
            }
            else
            {
                Debug.Fail("The collection should have been either for the master or the detail item properties.");
                CollectionChangedEventManager.RemoveListener(collection, this);
            }
        }
            private void ExportDataItemCore(DataGridContext dataGridContext, ClipboardExporterBase clipboardExporter, int itemIndex, object item,
                                            int[] exportedVisiblePositions, ColumnBase[] columnsByVisiblePosition)
            {
                var dataGridCollectionViewBase = dataGridContext.ItemsSourceCollection as DataGridCollectionViewBase;

                clipboardExporter.StartDataItem(dataGridContext, item);

                // Ensure the count does not exceeds the columns count
                var exportedVisiblePositionsCount = exportedVisiblePositions.Length;

                exportedVisiblePositionsCount = Math.Min(exportedVisiblePositionsCount, columnsByVisiblePosition.Length);

                var intersectedIndexes = this.GetIntersectedRangesForIndex(dataGridContext, itemIndex, exportedVisiblePositions, exportedVisiblePositionsCount);

                for (int i = 0; i < exportedVisiblePositionsCount; i++)
                {
                    var fieldValue      = default(object);
                    var column          = default(Column);
                    var visiblePosition = exportedVisiblePositions[i];

                    // Export null if not intersected by a SelectionRange
                    if (intersectedIndexes.Contains(visiblePosition))
                    {
                        // Only export visible data column
                        column = columnsByVisiblePosition[visiblePosition] as Column;

                        if (column == null)
                        {
                            continue;
                        }

                        // Use DataGridCollectionView directly since the DataGridItemProperty uses PropertyDescriptor which increase the read of the field value
                        var dataGridItemProperty = default(DataGridItemPropertyBase);

                        // Try to get a DataGridItemProperty matching the column FieldName and get the value from it
                        if (dataGridCollectionViewBase != null)
                        {
                            dataGridItemProperty = ItemsSourceHelper.GetItemPropertyFromProperty(dataGridCollectionViewBase.ItemProperties, column.FieldName);

                            if (dataGridItemProperty != null)
                            {
                                fieldValue = ItemsSourceHelper.GetValueFromItemProperty(dataGridItemProperty, item);
                            }
                        }

                        // If none was found, create a BindingPathValueExtractor from this column
                        if ((dataGridCollectionViewBase == null) || (dataGridItemProperty == null))
                        {
                            // We don't have a DataGridCollectionView, use a BindingPathValueExtractor to create a binding to help us get the value for the Column in the data item
                            var extractorForRead = default(BindingPathValueExtractor);

                            if (m_columnToBindingPathExtractor.TryGetValue(column, out extractorForRead) == false)
                            {
                                extractorForRead = dataGridContext.GetBindingPathExtractorForColumn(column, item);
                                m_columnToBindingPathExtractor.Add(column, extractorForRead);
                            }

                            fieldValue = extractorForRead.GetValueFromItem(item);
                        }
                    }

                    if (fieldValue != null)
                    {
                        //Verify if the value should be converted to the displayed value for exporting.
                        var foreignKeyConfiguration = column.ForeignKeyConfiguration;
                        if (foreignKeyConfiguration != null && foreignKeyConfiguration.UseDisplayedValueWhenExporting)
                        {
                            fieldValue = foreignKeyConfiguration.GetDisplayMemberValue(fieldValue);
                        }
                        else
                        {
                            var valueConverter = column.DisplayedValueConverter;
                            if (valueConverter != null)
                            {
                                fieldValue = valueConverter.Convert(fieldValue, typeof(object), column.DisplayedValueConverterParameter,
                                                                    column.GetCulture(column.DisplayedValueConverterCulture));
                            }
                            else
                            {
                                var valueFormat = column.CellContentStringFormat;
                                if (!string.IsNullOrEmpty(valueFormat))
                                {
                                    fieldValue = string.Format(column.GetCulture(), valueFormat, fieldValue);
                                }
                            }
                        }
                    }

                    clipboardExporter.StartDataItemField(dataGridContext, column, fieldValue);
                    clipboardExporter.EndDataItemField(dataGridContext, column, fieldValue);
                }

                clipboardExporter.EndDataItem(dataGridContext, item);
            }
        protected internal override void Initialize(DataGridCollectionViewBase parentCollectionView)
        {
            base.Initialize(parentCollectionView);

            if (this.PropertyDescriptor != null)
            {
                return;
            }

            string relationName = this.RelationName;

            if (String.IsNullOrEmpty(relationName) == true)
            {
                throw new InvalidOperationException("An attempt was made to initialize a PropertyDetailDescription whose Name property has not been set.");
            }

            foreach (DataGridDetailDescription detailDescription in parentCollectionView.DetailDescriptions.DefaultDetailDescriptions)
            {
                PropertyDetailDescription propertyDetailDescription = detailDescription as PropertyDetailDescription;
                if (propertyDetailDescription != null)
                {
                    if (propertyDetailDescription.RelationName == relationName)
                    {
                        this.PropertyDescriptor = propertyDetailDescription.PropertyDescriptor;
                        return;
                    }
                }
            }

            // The DetailDescription for RelationName was not found in DefaultDetailDescription
            // and may not have the PropertyDetailDescriptionAttribute
            if (this.PropertyDescriptor == null)
            {
                PropertyDescriptor relationDescriptor = null;

                IEnumerable enumeration = parentCollectionView as IEnumerable;

                if (enumeration != null)
                {
                    // Try to get it from the first item in the DataGridCollectionView
                    object firstItem = ItemsSourceHelper.GetFirstItemByEnumerable(enumeration);

                    if (firstItem != null)
                    {
                        relationDescriptor = this.GetPropertyDescriptorFromFirstItem(firstItem);

                        if (relationDescriptor != null)
                        {
                            this.PropertyDescriptor = relationDescriptor;
                            return;
                        }
                    }
                }

                // If the list is empty, check if the SourceCollection is ITypedList
                ITypedList iTypedList = parentCollectionView.SourceCollection as ITypedList;

                if (iTypedList != null)
                {
                    relationDescriptor = this.GetPropertyDescriptorFromITypedList(iTypedList);

                    if (relationDescriptor != null)
                    {
                        this.PropertyDescriptor = relationDescriptor;
                        return;
                    }
                }
            }

            throw new InvalidOperationException("An attempt was made to initialize a PropertyDetailDescription whose data source does not contain a property that corresponds to the specified relation name.");
        }
Ejemplo n.º 26
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);
                    }
                }
            }
        }