예제 #1
0
        public virtual PropertyValue.ValueValidationResult CommitChanges(bool final)
        {
            PropertyValue.ValueValidationResult changed = PropertyValue.ValueValidationResult.PreviousValueRestored;

            if ((_edit != null) && _edit.ReadOnly)
            {
                return(changed);
            }

            PropertyValue value = _ownerPropertyEnum.Property.Value;

            // Store the previous value
            object oldValue = null;

            if (value.HasMultipleValues == false)
            {
                oldValue = value.GetValue();
            }

            object newValue = null;

            try
            {
                if ((value.HasMultipleValues == false) || (Text.Length != 0))
                {
                    newValue = value.GetActualValue(Text);
                }
            }
            catch (Exception e)
            {
                _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                    new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text,
                                                 PropertyValue.ValueValidationResult.TypeConverterFailed, e));

                if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore)
                {
                    Text = value.DisplayString;
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     value.GetValue(), _currentValueValidationResult));
                }

                return(_currentValueValidationResult);
            }

            if (((newValue != null) && ((oldValue == null) || (newValue.Equals(oldValue) == false))) ||
                ((newValue == null) && (oldValue != null)))
            {
                _ownerPropertyEnum.Property.Value.PreviousValue = _ownerPropertyEnum.Property.Value.GetValue();

                PropertyEnumerator invalidPropertyEnum;
                changed = value.SetValueFromInPlaceCtrl(Text, out invalidPropertyEnum);

                if (PropertyValue.IsErrorCode(changed))
                {
                    _currentInvalidPropertyEnum = invalidPropertyEnum;
                }

                _currentValueValidationResult = changed;

                // Notify the grid if the value is invalid
                if (changed == PropertyValue.ValueValidationResult.ValidatorFailed)
                {
                    OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     newValue, PropertyValue.ValueValidationResult.ValidatorFailed));
                }
                else if (changed == PropertyValue.ValueValidationResult.ExceptionByClient)
                {
                    OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     newValue, PropertyValue.ValueValidationResult.ExceptionByClient, value.LastExceptionByClient));
                }

                // We have to update the edit control in case the value was refused or if its string representation
                // is different from what was typed (changes done by a CultureInfo for example)
                if ((_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode ==
                     PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore) ||
                    (changed == PropertyValue.ValueValidationResult.Validated))
                {
                    Text = value.DisplayString;
                    if (final)
                    {
                        _initialValue = _trackbar.Value;
                    }
                }

                // If value is invalid, previous value is restored if ValueNotValidBehaviorMode is set accordingly
                if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore)
                {
                    if (PropertyValue.IsErrorCode(changed))
                    {
                        _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored;
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                         value.GetValue(), _currentValueValidationResult));
                    }
                }

                if (changed == PropertyValue.ValueValidationResult.Validated)
                {
                    if (PropertyValue.IsErrorCode(_currentValueValidationResult))
                    {
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                         value.GetValue(), changed));
                    }

                    _trackbar.Value = (int)newValue;

                    _parentGrid.NotifyPropertyChanged(new PropertyChangedEventArgs(_ownerPropertyEnum));
                }

                Parent.Refresh();
            }
            else
            {
                // Even if the value did not change, we ensure the text displayed is correct. A CultureInfo could
                // be involved for example that would change a modified text into the old value.
                Text = value.DisplayString;

                if (PropertyValue.IsErrorCode(_currentValueValidationResult))
                {
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     value.GetValue(), _currentValueValidationResult));
                }
            }

            return(changed);
        }
예제 #2
0
        public virtual PropertyValue.ValueValidationResult CommitChanges(bool final)
        {
            PropertyValue.ValueValidationResult changed = PropertyValue.ValueValidationResult.PreviousValueRestored;

            PropertyValue value = _ownerPropertyEnum.Property.Value;

            bool compareTextInCommitChanges = value.TypeConverter.CanConvertFrom(typeof(string));
            bool textHasChanged             = false;

            if (compareTextInCommitChanges)
            {
                textHasChanged = !value.DisplayString.Equals(Text);
            }

            object oldValue       = null;
            object newValue       = null;
            bool   multipleValues = value.HasMultipleValues;

            if (!compareTextInCommitChanges || textHasChanged)
            {
                // Store the previous value
                if (!multipleValues)
                {
                    oldValue = value.GetValue();
                }

                // Assign the new value based on the edit content
                try
                {
                    if (!multipleValues || (Text.Length != 0))
                    {
                        newValue = value.GetActualValue(Text);
                    }
                }
                catch (Exception e)
                {
                    _currentInvalidPropertyEnum   = _ownerPropertyEnum;
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text,
                                                     PropertyValue.ValueValidationResult.TypeConverterFailed, e));

                    if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore)
                    {
                        Text = value.DisplayString;
                        _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored;
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                         value.GetValue(), _currentValueValidationResult));
                    }

                    return(_currentValueValidationResult);
                }
            }

            if ((multipleValues && textHasChanged) || ((newValue != null) && ((oldValue == null) || (newValue.Equals(oldValue) == false))) ||
                ((newValue == null) && (oldValue != null)))
            {
                // Freeze painting because updating the property may also update other properties in the grid.
                _ownerPropertyEnum.Property.ParentGrid.BeginUpdate();

                _ownerPropertyEnum.Property.Value.PreviousValue = _ownerPropertyEnum.Property.Value.GetValue();

                PropertyEnumerator invalidPropertyEnum;
                changed = value.SetValueFromInPlaceCtrl(Text, out invalidPropertyEnum);

                if (PropertyValue.IsErrorCode(changed))
                {
                    _currentInvalidPropertyEnum = invalidPropertyEnum;
                }

                // Notify the grid if the value is invalid
                if (changed == PropertyValue.ValueValidationResult.ValidatorFailed)
                {
                    OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     newValue, PropertyValue.ValueValidationResult.ValidatorFailed));
                }
                else if (changed == PropertyValue.ValueValidationResult.ExceptionByClient)
                {
                    OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     newValue, PropertyValue.ValueValidationResult.ExceptionByClient, value.LastExceptionByClient));
                }

                // We have to update the edit control in case the value was refused or if its string representation
                // is different from what was typed (changes done by a CultureInfo for example)
                if ((_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode ==
                     PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore) ||
                    (changed == PropertyValue.ValueValidationResult.Validated))
                {
                    Text = value.DisplayString;
                    if (final)
                    {
                        _initialValue = Text;
                    }
                }

                // If value is invalid, previous value is restored if ValueNotValidBehaviorMode is set accordingly
                if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore)
                {
                    if (PropertyValue.IsErrorCode(changed))
                    {
                        changed = PropertyValue.ValueValidationResult.PreviousValueRestored;
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                         value.GetValue(), changed));
                    }
                }

                // Notify the properties control if the value has really changed
                if (changed == PropertyValue.ValueValidationResult.Validated)
                {
                    if (PropertyValue.IsErrorCode(_currentValueValidationResult))
                    {
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                         value.GetValue(), changed));
                    }

                    PropertyChangedEventArgs e = new PropertyChangedEventArgs(_ownerPropertyEnum);
                    e.SelectedIndex = _ownerPropertyEnum.Property.Value.ImageIndex;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyPropertyChanged(e);
                }

                if (final)
                {
                    _oldImageListIndex = _ownerPropertyEnum.Property.Value.ImageIndex;
                }
                _currentValueValidationResult = changed;

                // Defreeze painting and redraw
                _ownerPropertyEnum.Property.ParentGrid.EndUpdate();
            }
            else
            {
                // Even if the value did not change, we ensure the text displayed is correct. A CultureInfo could
                // be involved for example that would change a modified text into the old value.
                Text = value.DisplayString;

                if (PropertyValue.IsErrorCode(_currentValueValidationResult))
                {
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     value.GetValue(), _currentValueValidationResult));
                }

                if (final)
                {
                    _oldImageListIndex = _ownerPropertyEnum.Property.Value.ImageIndex;
                    _initialValue      = Text;
                }
            }

            return(changed);
        }
예제 #3
0
        public virtual PropertyValue.ValueValidationResult CommitChanges(bool final)
        {
            PropertyValue.ValueValidationResult changed = PropertyValue.ValueValidationResult.PreviousValueRestored;

            if (ReadOnly)
            {
                return(changed);
            }

            PropertyValue value = _ownerPropertyEnum.Property.Value;

            if (value != null)
            {
                string oldMask = Mask;
                Mask = "";
                string dislayedText = Text;
                Mask = oldMask;

                // Store the previous value
                object oldValue = null;
                if (value.HasMultipleValues == false)
                {
                    oldValue = value.GetValue();
                }

                // Assign the new value based on the edit content
                object newValue = null;
                try
                {
                    if ((value.HasMultipleValues == false) || (dislayedText.Length != 0))
                    {
                        newValue = value.GetActualValue(dislayedText);
                    }
                }
                catch (Exception e)
                {
                    _currentInvalidPropertyEnum   = _ownerPropertyEnum;
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.TypeConverterFailed;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum, Text,
                                                     PropertyValue.ValueValidationResult.TypeConverterFailed, e));

                    if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore)
                    {
                        Text = value.DisplayString;
                        _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored;
                        _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                            new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                         value.GetValue(), _currentValueValidationResult));
                    }

                    return(_currentValueValidationResult);
                }

                if (((newValue != null) && ((oldValue == null) || (newValue.Equals(oldValue) == false))) ||
                    ((newValue == null) && (oldValue != null)))
                {
                    if (MaskCompleted)
                    {
                        // Freeze painting because updating the property may also update other properties in the grid.
                        // If the value is invalid based on the validator, all updated properties will be reverted back to
                        // the previous value and a flicker would happen
                        _ownerPropertyEnum.Property.ParentGrid.BeginUpdate();

                        _ownerPropertyEnum.Property.Value.PreviousValue = _ownerPropertyEnum.Property.Value.GetValue();

                        PropertyEnumerator invalidPropertyEnum;
                        changed = value.SetValueFromInPlaceCtrl(dislayedText, out invalidPropertyEnum);
                        if (changed == PropertyValue.ValueValidationResult.ValidatorFailed)
                        {
                            _currentInvalidPropertyEnum = invalidPropertyEnum;
                        }

                        // Notify the grid if the value is invalid
                        if (changed == PropertyValue.ValueValidationResult.ValidatorFailed)
                        {
                            OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                             newValue, PropertyValue.ValueValidationResult.ValidatorFailed));
                        }
                        else if (changed == PropertyValue.ValueValidationResult.ExceptionByClient)
                        {
                            OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                             newValue, PropertyValue.ValueValidationResult.ExceptionByClient, value.LastExceptionByClient));
                        }

                        // We have to update the edit control in case the value was refused or if its string representation
                        // is different from what was typed (changes done by a CultureInfo for example)
                        if ((_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode ==
                             PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore) ||
                            (changed == PropertyValue.ValueValidationResult.Validated))
                        {
                            Text = value.DisplayString;
                        }

                        // If value is invalid, previous value is restored if ValueNotValidBehaviorMode is set accordingly
                        if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode == PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore)
                        {
                            if (PropertyValue.IsErrorCode(changed))
                            {
                                changed = PropertyValue.ValueValidationResult.PreviousValueRestored;
                                _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                                    new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                                 value.GetValue(), changed));
                            }
                        }
                    }
                    else
                    {
                        if (_ownerPropertyEnum.Property.ParentGrid.ValueNotValidBehaviorMode ==
                            PropertyGrid.ValueNotValidBehaviorModes.IgnoreAndRestore)
                        {
                            OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, OwnerPropertyEnumerator,
                                                             newValue, PropertyValue.ValueValidationResult.ValidatorFailed));

                            Text = value.DisplayString;

                            _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, OwnerPropertyEnumerator,
                                                             value.GetValue(), changed));
                        }
                        else
                        {
                            changed = PropertyValue.ValueValidationResult.ValidatorFailed;
                            _currentInvalidPropertyEnum   = OwnerPropertyEnumerator;
                            PropertyValidatorBase.Message = "Incomplete mask";
                            OwnerPropertyEnumerator.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                             newValue, changed));
                        }
                    }

                    // Notify the properties control if the value has really changed
                    if (changed == PropertyValue.ValueValidationResult.Validated)
                    {
                        if (PropertyValue.IsErrorCode(_currentValueValidationResult))
                        {
                            _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                                new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                             value.GetValue(), changed));
                        }

                        _ownerPropertyEnum.Property.ParentGrid.NotifyPropertyChanged(new PropertyChangedEventArgs(_ownerPropertyEnum));
                    }

                    _currentValueValidationResult = changed;

                    // Defreeze painting and repaint
                    _ownerPropertyEnum.Property.ParentGrid.EndUpdate();
                }
            }
            else
            {
                // Even if the value did not change, we ensure the text displayed is correct. A CultureInfo could
                // be involved for example that would change a modified text into the old value.
                Text = value.DisplayString;

                if (PropertyValue.IsErrorCode(_currentValueValidationResult))
                {
                    _currentValueValidationResult = PropertyValue.ValueValidationResult.PreviousValueRestored;
                    _ownerPropertyEnum.Property.ParentGrid.NotifyValueValidation(
                        new ValueValidationEventArgs(OwnerPropertyEnumerator, _currentInvalidPropertyEnum,
                                                     value.GetValue(), _currentValueValidationResult));
                }
            }

            return(changed);
        }