示例#1
0
    public static void GenerateColumnsFromItemsSourceFields(
      ColumnCollection columns,
      IDictionary<Type, CellEditor> defaultCellEditors,
      Dictionary<string, FieldDescriptor> fields,
      bool autoCreateForeignKeyConfigurations )
    {
      DataGridControl dataGridControl = columns.DataGridControl;

      using( columns.DeferColumnAdditionMessages() )
      {
        foreach( FieldDescriptor field in fields.Values )
        {
          string fieldName = field.Name;
          ColumnBase column = columns[ fieldName ];
          Column dataColumn = column as Column;
          Type dataType = field.DataType;

          if( column == null )
          {
            dataColumn = ItemsSourceHelper.CreateColumnFromItemsSourceField(
              dataGridControl, defaultCellEditors, field, autoCreateForeignKeyConfigurations );

            if( dataColumn != null )
            {
              columns.Add( dataColumn );
              ItemsSourceHelper.ApplySettingsRepositoryToColumn( dataColumn );
            }
          }
          else if( dataColumn != null )
          {
            if( field.ReadOnly )
            {
              if( dataColumn.ReadLocalValue( Column.ReadOnlyProperty ) == DependencyProperty.UnsetValue )
              {
                dataColumn.ReadOnly = field.ReadOnly;
              }
            }

            if( field.OverrideReadOnlyForInsertion )
            {
              if( dataColumn.ReadLocalValue( ColumnBase.OverrideReadOnlyForInsertionProperty ) == DependencyProperty.UnsetValue )
              {
                dataColumn.OverrideReadOnlyForInsertion = field.OverrideReadOnlyForInsertion;
              }
            }

            if( dataColumn.ReadLocalValue( Column.TitleProperty ) == DependencyProperty.UnsetValue )
            {
              dataColumn.Title = field.DisplayName;
            }

            if( dataColumn.ReadLocalValue( Column.CellEditorProperty ) == DependencyProperty.UnsetValue )
            {
              CellEditor cellEditor = null;

              if( defaultCellEditors != null )
              {
                defaultCellEditors.TryGetValue( dataType, out cellEditor );
              }

              if( cellEditor == null )
              {
                object descriptionItemsSource = null;
                object configurationItemsSource = null;
                ForeignKeyConfiguration configuration = dataColumn.ForeignKeyConfiguration;

                if( field.ForeignKeyDescription != null )
                {
                  descriptionItemsSource = field.ForeignKeyDescription.ItemsSource;
                }

                if( configuration != null )
                {
                  configurationItemsSource = configuration.ItemsSource;

                  if( configurationItemsSource == null )
                  {
                    configurationItemsSource = dataColumn.ReadLocalValue( Column.ForeignKeyConfigurationProperty );
                  }
                }

                // A foreign key ItemsSource is set and we can auto-create configuration
                // OR
                // if the foreign key ItemsSource was found in the ForeignKeyConfiguration
                //
                // use the Default ForeignKey CellEditor.
                if( ( ( descriptionItemsSource != null ) && ( autoCreateForeignKeyConfigurations ) )
                    || ( configurationItemsSource != null ) )
                {
                  cellEditor = DefaultCellEditorSelector.ForeignKeyCellEditor;
                }
              }

              if( cellEditor == null )
              {
                cellEditor = DefaultCellEditorSelector.SelectCellEditor( dataType );
              }

              dataColumn.CellEditor = cellEditor;
            }

            if( ( field.ForeignKeyDescription != null )
                && ( field.ForeignKeyDescription.ItemsSource != null )
                && ( autoCreateForeignKeyConfigurations ) )
            {
              // Update the ForeignKeyConfiguration from the ForeignKeyDescription
              // found on the FieldDescriptor
              ForeignKeyConfiguration.SynchronizeForeignKeyConfigurationFromForeignKeyDescription(
                dataColumn,
                field.ForeignKeyDescription,
                autoCreateForeignKeyConfigurations );
            }

            // Disable warning for DisplayMemberBinding when internaly used
#pragma warning disable 618

            if( dataColumn.DisplayMemberBinding == null )
            {
              dataColumn.DisplayMemberBinding = ItemsSourceHelper.CreateDefaultBinding(
                false, field.Name, field,
                false, ( dataColumn.ReadOnly && !dataColumn.OverrideReadOnlyForInsertion ), dataType );

              //mark the Column's Binding as AutoCreated.
              dataColumn.IsBindingAutoCreated = true;
              dataColumn.IsBoundToDataGridUnboundItemProperty = field.IsDataGridUnboundItemProperty;
            }

#pragma warning restore 618
          }
        }
      } //end using
    }