コード例 #1
0
        private SourceProperty GetSourceProperty(string path, object container)
        {
            string[] parts           = path.Split('.');
            string   leftMost        = parts[0];
            string   leftMostName    = GetName(leftMost);
            int      leftMostIndexer = 0;

            SourceProperty result = new SourceProperty {
                OwningInstance = container
            };

            if (!string.IsNullOrEmpty(leftMost))
            {
                PropertyDescriptor pDescriptor = TypeDescriptor.GetProperties(container).Find(leftMostName, true);
                if (pDescriptor != null)
                {
                    result.Descriptor = pDescriptor;

                    object pValue = pDescriptor.GetValue(container);

                    //this is the dataitem from the viewstate
                    IEnumerable enumerablePValue = pValue as IEnumerable;
                    if (enumerablePValue != null && !(pValue is string))
                    {
                        if (TryGetIndexer(leftMost, out leftMostIndexer))
                        {
                            pValue = enumerablePValue.GetByIndex(leftMostIndexer);
                        }
                    }

                    if (pValue != null)
                    {
                        result.Value = pValue;
                        //remove the current section and retest
                        path = path.TrimStart(parts[0].ToCharArray());
                        path = path.TrimStart(new char[] { '.' });
                        string[] newParts = path.Split('.');
                        if (newParts[0] != string.Empty)
                        {
                            result = GetSourceProperty(path, pValue);
                        }

                        return(result);
                    }
                }
            }

            return(result);
        }
コード例 #2
0
        //TODO: Should probably be a member of binding.
        private void SetProperty(BindingDef binding, object value)
        {
            SourceProperty property = binding.SourceExpression.ResolveAsSource(this.DataContext);

            if (property != null && property.Descriptor != null && !property.Descriptor.IsReadOnly)
            {
                object convertedValue  = null;
                Type   destinationType = property.Descriptor.PropertyType;

                if (binding.HasValueConverter)
                {
                    IValueConverter valueConverter = binding.GetValueConverterInstance();

                    convertedValue = valueConverter.ConvertBack(value, property.Descriptor.PropertyType, null, null);
                }
                else
                {
                    if (TypeHelper.IsSimpleType(value.GetType()) && TypeHelper.IsSimpleType(destinationType))
                    {
                        convertedValue = Convert.ChangeType(value, destinationType);
                    }

                    else
                    {
                        TypeConverter converter = TypeDescriptor.GetConverter(destinationType);
                        if (converter != null)
                        {
                            convertedValue = converter.ConvertFrom(value);
                        }
                        else
                        {
                            converter = TypeDescriptor.GetConverter(value.GetType());

                            if (converter != null)
                            {
                                convertedValue = converter.ConvertTo(value, destinationType);
                            }
                        }
                    }
                }

                property.Descriptor.SetValue(property.OwningInstance, convertedValue);
            }
        }
コード例 #3
0
        private void ExecuteCommandBind(IBindingTarget control, BindingDef binding)
        {
            //Get the ICommandObject from the source
            SourceProperty commandInstance = binding.SourceExpression.ResolveAsSource(this.DataContext);
            object         commandObject   = commandInstance.Value;
            ICommand       iCommand        = commandObject as ICommand;

            //Get the property setter for the event to which we will bind
            TargetEvent evt = binding.ResolveAsTargetEvent(this.bindingContainer);

            //bind
            Type     eventDelegateType = evt.Descriptor.EventHandlerType;
            Delegate handler           = new EventHandler((s, e) => iCommand.Execute(this));

            evt.Descriptor.GetAddMethod().Invoke(evt.OwningControl, new object[] { handler });

            control.Visible = iCommand.CanExecute(this);

            //listen to canExecuteChanged
            iCommand.CanExecuteChanged += new EventHandler((s, e) => control.Visible = iCommand.CanExecute(this));
        }