Exemplo n.º 1
0
 internal void RaiseInitializeProperty(InitializePropertyDescriptorEventArgs e)
 {
     if (InitializeProperty != null)
     {
         InitializeProperty(this, e);
     }
 }
Exemplo n.º 2
0
        private void OnGoCreateNew(TaskGoEventArgs e)
        {
            object value = e.Instance.Argument.UserState;

            if (value == null)
            {
                return;
            }

            object valueDefault = pg.ObjectDefalult;
            Type   type         = null;

            PropertyInfo[] pis = null;
            if (valueDefault != null)
            {
                type = valueDefault.GetType();
            }
            if (type != null)
            {
                pis = type.GetProperties();
            }

            var list = new List <PropertyDescriptor>();

            value.TraversalPropertiesInfo((PropertyInfo pi, object val) =>
            {
                if (e.Instance.IsStopPending)
                {
                    return(false);
                }
                if (!pi.CanWrite)
                {
                    return(true);
                }

                PropertyDescriptor meta = null;

                Application.Current.Dispatcher.Invoke(
                    new Action(() => meta = PropertyDescriptor.Create(pg, value, pi.Name, pg.Editable, pg.PropertyClass, pg.ContainerType)));

                if (meta == null)
                {
                    return(true);
                }

                //meta.Object = value;
                meta.PropertyGrid = pg;

                if (pis != null && pis.Any(c => c.Name == pi.Name))
                {
                    meta.DefaultValue = valueDefault.GetPropertyValue(pi.Name);
                }

                var args = new InitializePropertyDescriptorEventArgs(meta);
                pg.RaiseInitializeProperty(args);
                if (!args.Cancel)
                {
                    list.Add(meta);
                    return(true);
                }

                var attr = pi.GetAttribute <PropertyDescriptorAttribute>();
                if (attr == null)
                {
                    list.Add(meta);
                    return(true);
                }

                var builder = attr.CreateBuilder();
                if (builder == null)
                {
                    list.Add(meta);
                    return(true);
                }

                meta = builder.Build(meta);
                if (meta != null)
                {
                    list.Add(meta);
                    return(true);
                }

                return(true);
            });

            var broadcast = false;

            Application.Current.Dispatcher.Invoke(new Action(() => broadcast = pg.BroadcastPropertyChanged));

            list.ForEach(c =>
            {
                Application.Current.Dispatcher.Invoke(new Action(() => pg._properties.Add(c)));
                c.Install();

                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    if (c.BindingExpression != null && !pg.IsGroupingEnabled && c.Editable)
                    {
                        pg.BindingGroup.BindingExpressions.Add(c.BindingExpression);
                    }
                }));

                if (broadcast)
                {
                    list.ForEach(d => c.PostPropertyValueChangedAsync(d.Name, true));
                }
            });

            //e.Instance.Argument.UserState = list;
        }