示例#1
0
        /// <summary>
        /// Gets the <see cref="ObjectField"/> which is eqivalent to the given field
        /// within this tree.
        /// </summary>
        /// <param name="field">The field to search the equivalent for.</param>
        /// <returns>The found field or null.</returns>
        public ObjectField GetEquivalentField(ObjectField field)
        {
            if (field == null || field.Parent == null)
            {
                return(TreeModel.Root);
            }
            ObjectField equivalentParentField = GetEquivalentField(field.Parent);
            ObjectField equivalentField       = null;

            ObjectFieldClass equivalentParentFieldClass = equivalentParentField as ObjectFieldClass;

            if (equivalentParentFieldClass != null)
            {
                equivalentField =
                    equivalentParentFieldClass.ChildFields.FirstOrDefault(childField => childField.Name == field.Name);
            }
            ObjectFieldList equivalentParentFieldList = equivalentParentField as ObjectFieldList;

            if (equivalentParentFieldList != null)
            {
                equivalentField = equivalentParentFieldList.Items.FirstOrDefault(item => item.Name == field.Name);
            }
            ObjectFieldDictionary equivalentParentFieldDictionary = equivalentParentField as ObjectFieldDictionary;

            if (equivalentParentFieldDictionary != null)
            {
                equivalentField = equivalentParentFieldDictionary.Items.Values.FirstOrDefault(item => item.Name == field.Name);
            }

            return(equivalentField);
        }
        /// <summary>
        /// Gets called after a value of a field has changed.
        /// </summary>
        /// <param name="sender">The sender of the event.</param>
        /// <param name="e">Informations about the event.</param>
        private void field_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            ObjectFieldClass fieldClass = e.ObjectField as ObjectFieldClass;

            if (fieldClass != null)
            {
                // The changed field is a class.
                ModelCreator.RefreshObjectField(fieldClass, e.NewValue);
                TreePath treePath = GetPath(e.ObjectField);
                OnStructureChanged(new TreePathEventArgs(treePath));
                treeView.FindNode(treePath).Expand();
            }
            Dirty = true;
            OnValueChanged(e);
        }
        private ObjectFieldClass CreateObjectFieldClass(string name, Type type, PropertyInfo property, object o)
        {
            ObjectFieldClass field = new ObjectFieldClass
            {
                Name         = name,
                IsNullable   = true,
                PropertyInfo = property,
                Type         = type
            };

            RefreshObjectField(field, o);

            field.ValueChanged += ValueChangedMethod;

            return(field);
        }
 /// <summary>
 /// Refreshes the contents of the <see cref="ObjectField"/>.
 /// </summary>
 /// <param name="field">The field to update.</param>
 /// <param name="o">The new object belonging to the field.</param>
 public void RefreshObjectField(ObjectFieldClass field, object o)
 {
     if (o == null)
     {
         field.ChildFields.Clear();
     }
     else
     {
         foreach (PropertyInfo innerProperty in o.GetType().GetProperties())
         {
             ObjectField innerField = CreateObjectField(innerProperty.Name, innerProperty.PropertyType, innerProperty.GetValue(o, null), innerProperty);
             if (innerField == null)
             {
                 continue;
             }
             innerField.Parent = field;
             field.ChildFields.Add(innerField);
         }
     }
     field.Value = o;
 }
        // ========================================================================
        // Methods

        #region === Methods

        /// <summary>
        /// Returns the children of a node at a specified path.
        /// </summary>
        /// <param name="treePath">The path.</param>
        /// <returns>The children.</returns>
        public override IEnumerable GetChildren(TreePath treePath)
        {
            ObjectField field;

            if (treePath.IsEmpty())
            {
                if (DisplayRoot == null)
                {
                    return(null);
                }
                field = DisplayRoot;
            }
            else
            {
                field = treePath.LastNode as ObjectField;
            }

            ObjectFieldClass objectFieldClass = field as ObjectFieldClass;

            if (objectFieldClass != null)
            {
                return(objectFieldClass.ChildFields);
            }
            ObjectFieldList objectFieldList = field as ObjectFieldList;

            if (objectFieldList != null)
            {
                return(objectFieldList.Items);
            }
            ObjectFieldDictionary objectFieldDictionary = field as ObjectFieldDictionary;

            if (objectFieldDictionary != null)
            {
                return(objectFieldDictionary.Items.Values);
            }
            return(null);
        }