public override object GetKeyFromValue( object value, ForeignKeyConfiguration configuration )
    {
      // When the Key changes, try to find the data row that has the new key.
      // If it is not found, return null.
      DataView dataView = configuration.ItemsSource as DataView;
      if( dataView == null )
      {
        DataTable dataTable = configuration.ItemsSource as DataTable;
        if( dataTable != null )
        {
          dataView = dataTable.DefaultView;
        }
      }

      if( dataView == null )
      {
        return value;
      }

      DataRowView dataRow = value as DataRowView;

      if( dataRow == null )
      {
        return null;
      }
      else
      {
        return dataRow[ dataView.Sort ];
      }
    }
Example #2
0
        internal static void SynchronizeForeignKeyConfigurationFromForeignKeyDescription(Column column, DataGridForeignKeyDescription description,
                                                                                         bool autoCreateForeignKeyConfigurations)
        {
            if ((description == null) || (column == null))
            {
                return;
            }

            ForeignKeyConfiguration configuration = column.ForeignKeyConfiguration;

            if (configuration == null)
            {
                if (!autoCreateForeignKeyConfigurations)
                {
                    return;
                }

                configuration = new ForeignKeyConfiguration();
                configuration.IsAutoCreated    = true;
                column.ForeignKeyConfiguration = configuration;
            }

            // ValuePath will be affected to the FieldName when the configuration is auto-created to be able to modify local source using foreign key value
            if (configuration.IsAutoCreated)
            {
                if (string.IsNullOrEmpty(configuration.ValuePath))
                {
                    configuration.ValuePath = description.ValuePath;
                }

                if (string.IsNullOrEmpty(configuration.DisplayMemberPath))
                {
                    configuration.DisplayMemberPath = description.DisplayMemberPath;
                }
            }

            // Affect the ItemsSource on the configuration if it is not already set
            if ((configuration.ItemsSource == null) && (description.ItemsSource != null))
            {
                configuration.ItemsSource = description.ItemsSource;
            }

            // Set the Converter if it was not locally set
            if (configuration.ForeignKeyConverter == null)
            {
                configuration.ForeignKeyConverter = description.ForeignKeyConverter;
            }
        }
Example #3
0
        private void UpdateContent(Group group)
        {
            DataGridContext dataGridContext = DataGridControl.GetDataGridContext(this);

            if (dataGridContext == null)
            {
                return;
            }

            if (group == null)
            {
                return;
            }

            string groupBy = group.GroupBy;

            if (string.IsNullOrEmpty(groupBy))
            {
                throw new DataGridInternalException("Group.GroupBy is null.");
            }

            Column column = dataGridContext.Columns[groupBy] as Column;

            object newContent = group.Value;

            if (column != null)
            {
                ForeignKeyConfiguration foreignKeyConfiguration = column.ForeignKeyConfiguration;

                if (foreignKeyConfiguration != null)
                {
                    // Ensure to set the Content converted according to the DataContext
                    // when a converter is present. Else, the DataContext will be displayed
                    if (foreignKeyConfiguration.ForeignKeyConverter != null)
                    {
                        newContent = foreignKeyConfiguration.ForeignKeyConverter.GetValueFromKey(
                            group.Value,
                            foreignKeyConfiguration);
                    }
                }
            }

            // We must update the template according
            // to values present on Column
            this.UpdateContentTemplate(column, group);

            this.Content = newContent;
        }
Example #4
0
        public override object GetValueFromKey(object key, ForeignKeyConfiguration configuration)
        {
            var itemsSource = configuration.ItemsSource;

            if (itemsSource == null)
            {
                return(key);
            }

            if ((itemsSource is DataView) || (itemsSource is DataTable))
            {
                return(new DataTableForeignKeyConverter().GetValueFromKey(key, configuration));
            }

            return(this.GetValueFromKeyCore(key, itemsSource, configuration.ValuePath, configuration.DisplayMemberPath));
        }
        internal static void UpdateColumnsForeignKeyConfigurations(
            ObservableColumnCollection columns,
            IEnumerable itemsSourceCollection,
            PropertyDescriptionRouteDictionary propertyDescriptions,
            bool autoCreateForeignKeyConfigurations)
        {
            var collectionViewBase = itemsSourceCollection as DataGridCollectionViewBase;

            if (collectionViewBase != null)
            {
                ForeignKeyConfiguration.UpdateColumnsForeignKeyConfigurationsFromDataGridCollectionView(columns, collectionViewBase.ItemProperties, autoCreateForeignKeyConfigurations);
            }
            else
            {
                ForeignKeyConfiguration.UpdateColumnsForeignKeyConfigurationsFromPropertyDescriptions(columns, propertyDescriptions, autoCreateForeignKeyConfigurations);
            }
        }
        private static void UpdateColumnsForeignKeyConfigurationsFromPropertyDescriptions(
            ObservableColumnCollection columns,
            PropertyDescriptionRouteDictionary propertyDescriptions,
            bool autoCreateForeignKeyConfigurations)
        {
            if ((columns == null) || (propertyDescriptions == null))
            {
                return;
            }

            foreach (var column in columns)
            {
                var targetColumn = column as Column;
                if (targetColumn == null)
                {
                    continue;
                }

                var key = PropertyRouteParser.Parse(targetColumn.FieldName);
                if (key == null)
                {
                    continue;
                }

                var propertyDescription = propertyDescriptions[key];
                if (propertyDescription == null)
                {
                    continue;
                }

                var foreignKeyDescription = propertyDescription.Current.ForeignKeyDescription;
                if (foreignKeyDescription == null)
                {
                    continue;
                }

                ForeignKeyConfiguration.SynchronizeForeignKeyConfigurationFromForeignKeyDescription(targetColumn, foreignKeyDescription, autoCreateForeignKeyConfigurations);
            }
        }
Example #7
0
        // If a DataGridCollectionViewBase is used, get the ItemProperties it defines to be able to retreive DataGridForeignKeyDescription for each of them
        // to get the auto-detected ForeignKey ItemsSource (if any).
        // If a DataGridCollectionViewBase is not used, the ItemsSource must be manually specified on each Column in order to correctly display/edit the Data
        internal static void UpdateColumnsForeignKeyConfigurationsFromDataGridCollectionView(Dictionary <string, ColumnBase> columns, DataGridItemPropertyCollection itemProperties,
                                                                                             bool autoCreateForeignKeyConfigurations)
        {
            if (itemProperties == null)
            {
                return;
            }

            foreach (DataGridItemPropertyBase itemProperty in itemProperties)
            {
                DataGridForeignKeyDescription description = itemProperty.ForeignKeyDescription;

                if (description == null)
                {
                    continue;
                }

                ColumnBase column;
                columns.TryGetValue(itemProperty.Name, out column);

                ForeignKeyConfiguration.SynchronizeForeignKeyConfigurationFromForeignKeyDescription(column as Column, description, autoCreateForeignKeyConfigurations);
            }
        }
        internal static void UpdateColumnsForeignKeyConfigurations(
            Dictionary <string, ColumnBase> columns,
            IEnumerable itemsSourceCollection,
            Dictionary <string, ItemsSourceHelper.FieldDescriptor> fieldDescriptors,
            bool autoCreateForeignKeyConfigurations)
        {
            DataGridCollectionViewBase collectionViewBase =
                itemsSourceCollection as DataGridCollectionViewBase;

            if (collectionViewBase != null)
            {
                ForeignKeyConfiguration.UpdateColumnsForeignKeyConfigurationsFromDataGridCollectionView(
                    columns,
                    collectionViewBase.ItemProperties,
                    autoCreateForeignKeyConfigurations);
            }
            else
            {
                ForeignKeyConfiguration.UpdateColumnsForeignKeyConfigurationsFromFieldDescriptors(
                    columns,
                    fieldDescriptors,
                    autoCreateForeignKeyConfigurations);
            }
        }
 public virtual object GetKeyFromValue( object value, ForeignKeyConfiguration configuration )
 {
   return value;
 }
 public virtual object GetValueFromKey( object key, ForeignKeyConfiguration configuration )
 {
   return key;
 }
 internal CellEditorContext( ColumnBase parentColumn, ForeignKeyConfiguration configuration )
 {
   this.SetParentColumn( parentColumn );
   this.SetForeignKeyConfiguration( configuration );
 } 
Example #12
0
 public virtual object GetValueFromKey(object key, ForeignKeyConfiguration configuration)
 {
     return(key);
 }
Example #13
0
        private void RefreshDefaultScrollTipContentTemplate()
        {
            DataGridContext itemContext = DataGridControl.GetDataGridContext(this);

            if (itemContext == null)
            {
                return;
            }

            DataGridControl parentGridControl = itemContext.DataGridControl;

            if ((parentGridControl == null) || (parentGridControl.ScrollViewer == null))
            {
                return;
            }

            ColumnCollection columnCollection = itemContext.Columns;

            if (columnCollection == null)
            {
                return;
            }

            Column mainColumn = columnCollection.MainColumn as Column;

            if (mainColumn == null)
            {
                return;
            }

            Xceed.Wpf.DataGrid.Views.UIViewBase uiViewBase = parentGridControl.GetView() as Xceed.Wpf.DataGrid.Views.UIViewBase;

            if (uiViewBase == null)
            {
                return;
            }

            // The ScrollTip.ContentTemplate will now be set. This is to avoid
            // a null ContentTemplate when the ColumnsCollection update its
            // MainColumn after the template is applied
            this.ScrollTipContentTemplateNeedsRefresh = false;

            ForeignKeyConfiguration configuration = mainColumn.ForeignKeyConfiguration;

            // Do not create default template only when none was created before and a template already exists
            if ((!this.UsingDefaultScrollTipContentTemplate) && (uiViewBase.ScrollTipContentTemplate != null))
            {
                if (configuration != null)
                {
                    this.ContentTemplate = configuration.DefaultScrollTipContentTemplate;
                }
                else
                {
                    // Clear the value in case we previously affected it
                    this.ClearValue(ScrollTip.ContentTemplateProperty);

                    // Set the default Binding values
                    this.SetBinding(ScrollTip.ContentTemplateProperty, this.DefaultScrollTipContentTemplateBinding);
                    this.SetBinding(ScrollTip.ContentTemplateSelectorProperty, this.DefaultScrollTipContentTemplateSelectorBinding);
                }
            }
            else
            {
                // A default ContentTemplate template is created using MainColumn as displayed data
                this.UsingDefaultScrollTipContentTemplate = true;

                // If a configuration was found, the default ContentTemplate will
                // be used to convert Content to Foreign value and
                // it will use the ScrollTipContentTemplate defined on UIViewBase
                // if any
                if (configuration == null)
                {
                    // Disable warning for DisplayMemberBinding when internaly used
#pragma warning disable 618

                    BindingBase displayMemberBinding = mainColumn.DisplayMemberBinding;

#pragma warning restore 618

                    FrameworkElementFactory contentPresenter = new FrameworkElementFactory(typeof(ContentPresenter));
                    contentPresenter.SetValue(ContentPresenter.NameProperty, "defaultScrollTipDataTemplateContentPresenter");
                    contentPresenter.SetBinding(ContentPresenter.ContentProperty, displayMemberBinding);

                    contentPresenter.SetBinding(ContentPresenter.ContentTemplateProperty, this.DefaultCellContentTemplateBinding);
                    contentPresenter.SetBinding(ContentPresenter.ContentTemplateSelectorProperty, this.DefaultCellContentTemplateSelectorBinding);

                    DataTemplate template = new DataTemplate();
                    template.VisualTree = contentPresenter;
                    template.Seal();

                    this.ContentTemplate = template;
                }
                else
                {
                    this.SetBinding(ContentPresenter.ContentTemplateProperty, this.DefaultCellContentTemplateBinding);
                    this.SetBinding(ContentPresenter.ContentTemplateSelectorProperty, this.DefaultCellContentTemplateSelectorBinding);
                    this.ContentTemplate = configuration.DefaultScrollTipContentTemplate;
                }
            }
        }
 public override object GetValueFromKey(object key, ForeignKeyConfiguration configuration)
 {
     return(this.GetValueFromKeyCore(key, configuration.ItemsSource, configuration.ValuePath, configuration.DisplayMemberPath, true));
 }
 public static void RemoveListener( ForeignKeyConfiguration source, IWeakEventListener listener )
 {
   CurrentManager.ProtectedRemoveListener( source, listener );
 }
Example #16
0
        public override object GetKeyFromValue(object value, ForeignKeyConfiguration configuration)
        {
            if (value == null)
            {
                return(null);
            }

            var displayMemberPath = configuration.DisplayMemberPath;

            if (string.IsNullOrWhiteSpace(displayMemberPath))
            {
                return(value);
            }

            var valuePath = configuration.ValuePath;

            if (string.IsNullOrWhiteSpace(valuePath))
            {
                return(value);
            }

            var itemsSource = configuration.ItemsSource;

            if (itemsSource == null)
            {
                return(value);
            }

            if ((itemsSource is DataView) || (itemsSource is DataTable))
            {
                return(new DataTableForeignKeyConverter().GetKeyFromValue(value, configuration));
            }

            try
            {
                var enumerator = itemsSource.GetEnumerator();
                enumerator.MoveNext();
                var firstItem    = enumerator.Current;
                var itemType     = firstItem.GetType();
                var propertyInfo = itemType.GetProperty(displayMemberPath);

                foreach (object item in itemsSource)
                {
                    if (item == null)
                    {
                        continue;
                    }

                    var displayValue = propertyInfo.GetValue(item, null);

                    if (value.Equals(displayValue))
                    {
                        return(itemType.GetProperty(valuePath).GetValue(item, null));
                    }
                }
            }
            catch
            {
                //Swallow the exception, no need to terminate the application, just return the original value.
            }

            return(value);
        }
    public override object GetValueFromKey( object key, ForeignKeyConfiguration configuration )
    {
      // When the Key changes, try to find the data row that has the new key.
      // If it is not found, return null.
      DataView dataView = configuration.ItemsSource as DataView;
      if( dataView == null )
      {
        DataTable dataTable = configuration.ItemsSource as DataTable;
        if( dataTable != null )
        {
          dataView = dataTable.DefaultView;
        }
      }

      if( dataView == null )
      {
        return key;
      }

      string valuePath = configuration.ValuePath;

      if( string.IsNullOrEmpty( valuePath ) )
      {
        return key;
      }

      dataView.Sort = valuePath;

      DataRowView dataRow;

      int index = -1;

      if( ( key != null ) && ( !object.Equals( string.Empty, key ) ) )
      {
        try
        {
          index = dataView.Find( key );
        }
        catch( Exception )
        {
        }
      }

      if( index == -1 )
      {
        dataRow = null;
      }
      else
      {
        dataRow = dataView[ index ];
      }

      object value = dataRow;

      // Use the DisplayMemberPath if defined
      if( dataRow != null )
      {
        if( !string.IsNullOrEmpty( configuration.DisplayMemberPath ) )
        {
          value = dataRow[ configuration.DisplayMemberPath ];
        }
      }

      return value;
    }
        public override object GetValueFromKey(object key, ForeignKeyConfiguration configuration)
        {
            // When the Key changes, try to find the data row that has the new key.
            // If it is not found, return null.
            DataView dataView = configuration.ItemsSource as DataView;

            if (dataView == null)
            {
                DataTable dataTable = configuration.ItemsSource as DataTable;
                if (dataTable != null)
                {
                    dataView = dataTable.DefaultView;
                }
            }

            if (dataView == null)
            {
                return(key);
            }

            string valuePath = configuration.ValuePath;

            if (string.IsNullOrEmpty(valuePath))
            {
                return(key);
            }

            dataView.Sort = valuePath;

            DataRowView dataRow;

            int index = -1;

            if ((key != null) && (!object.Equals(string.Empty, key)))
            {
                try
                {
                    index = dataView.Find(key);
                }
                catch (Exception)
                {
                }
            }

            if (index == -1)
            {
                dataRow = null;
            }
            else
            {
                dataRow = dataView[index];
            }

            object value = dataRow;

            // Use the DisplayMemberPath if defined
            if (dataRow != null)
            {
                if (!string.IsNullOrEmpty(configuration.DisplayMemberPath))
                {
                    value = dataRow[configuration.DisplayMemberPath];
                }
            }

            return(value);
        }
    internal static void SynchronizeForeignKeyConfigurationFromForeignKeyDescription(
      Column column,
      DataGridForeignKeyDescription description,
      bool autoCreateForeignKeyConfigurations )
    {
      if( ( description == null ) || ( column == null ) )
        return;

      ForeignKeyConfiguration configuration = column.ForeignKeyConfiguration;

      if( configuration == null )
      {
        if( !autoCreateForeignKeyConfigurations )
          return;

        configuration = new ForeignKeyConfiguration();
        configuration.IsAutoCreated = true;
        column.ForeignKeyConfiguration = configuration;
      }

      // ValuePath will be affected to the FieldName when the 
      // configuration is auto-created to be able to modify 
      // local source using foreign key value
      if( configuration.IsAutoCreated )
      {
        if( string.IsNullOrEmpty( configuration.ValuePath ) )
        {
          configuration.ValuePath = description.ValuePath;
        }
      }

      // Affect the ItemsSource on the configuration if it is not
      // already set
      if( ( configuration.ItemsSource == null )
        && ( description.ItemsSource != null ) )
      {
        configuration.ItemsSource = description.ItemsSource;
      }

      // Set the Converter if it was not locally set
      if( configuration.ForeignKeyConverter == null )
      {
        configuration.ForeignKeyConverter = description.ForeignKeyConverter;
      }
    }
 public static void RemoveListener(ForeignKeyConfiguration source, IWeakEventListener listener)
 {
     CurrentManager.ProtectedRemoveListener(source, listener);
 }
Example #21
0
 public virtual object GetKeyFromValue(object value, ForeignKeyConfiguration configuration)
 {
     return(value);
 }
Example #22
0
 internal CellEditorContext(ColumnBase parentColumn, ForeignKeyConfiguration configuration)
 {
     this.SetParentColumn(parentColumn);
     this.SetForeignKeyConfiguration(configuration);
 }
 private void SetForeignKeyConfiguration( ForeignKeyConfiguration value )
 {
   this.SetValue( CellEditorContext.ForeignKeyConfigurationPropertyKey, value );
 }
Example #24
0
 private void SetForeignKeyConfiguration(ForeignKeyConfiguration value)
 {
     this.SetValue(CellEditorContext.ForeignKeyConfigurationPropertyKey, value);
 }