Example #1
0
        /// Edits the specified value using the specified provider within the specified context.
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (provider == null)
            {
                return(value);
            }

            IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (edSvc == null)
            {
                return(value);
            }

            DataGridViewCellStyle dgvCellStyle = context.Instance as DataGridViewCellStyle;
            ListControl           listControl  = context.Instance as ListControl;

            Debug.Assert(listControl != null || dgvCellStyle != null, "this editor is used for the DataGridViewCellStyle::Format and the ListControl::FormatString properties");
            Application.SetHighDpiMode(HighDpiMode.SystemAware);

            if (_formatStringDialog == null)
            {
                _formatStringDialog = new FormatStringDialog(context);
            }

            if (listControl != null)
            {
                _formatStringDialog.ListControl = listControl;
            }
            else
            {
                _formatStringDialog.DataGridViewCellStyle = dgvCellStyle;
            }

            IComponentChangeService changeSvc = (IComponentChangeService)provider.GetService(typeof(IComponentChangeService));

            if (changeSvc != null)
            {
                if (dgvCellStyle != null)
                {
                    changeSvc.OnComponentChanging(dgvCellStyle, TypeDescriptor.GetProperties(dgvCellStyle)["Format"]);
                    changeSvc.OnComponentChanging(dgvCellStyle, TypeDescriptor.GetProperties(dgvCellStyle)["NullValue"]);
                    changeSvc.OnComponentChanging(dgvCellStyle, TypeDescriptor.GetProperties(dgvCellStyle)["FormatProvider"]);
                }
                else
                {
                    changeSvc.OnComponentChanging(listControl, TypeDescriptor.GetProperties(listControl)["FormatString"]);
                    changeSvc.OnComponentChanging(listControl, TypeDescriptor.GetProperties(listControl)["FormatInfo"]);
                }
            }

            edSvc.ShowDialog(_formatStringDialog);
            _formatStringDialog.End();

            if (!_formatStringDialog.Dirty)
            {
                return(value);
            }

            // since the bindings may have changed, the properties listed in the properties window need to be refreshed
            TypeDescriptor.Refresh(context.Instance);

            if (changeSvc != null)
            {
                if (dgvCellStyle != null)
                {
                    changeSvc.OnComponentChanged(dgvCellStyle, TypeDescriptor.GetProperties(dgvCellStyle)["Format"], null, null);
                    changeSvc.OnComponentChanged(dgvCellStyle, TypeDescriptor.GetProperties(dgvCellStyle)["NullValue"], null, null);
                    changeSvc.OnComponentChanged(dgvCellStyle, TypeDescriptor.GetProperties(dgvCellStyle)["FormatProvider"], null, null);
                }
                else
                {
                    changeSvc.OnComponentChanged(listControl, TypeDescriptor.GetProperties(listControl)["FormatString"], null, null);
                    changeSvc.OnComponentChanged(listControl, TypeDescriptor.GetProperties(listControl)["FormatInfo"], null, null);
                }
            }

            return(value);
        }
        /// Edits the specified value using the specified provider within the specified context.
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (!provider.TryGetService(out IWindowsFormsEditorService editorService))
            {
                return(value);
            }

            var cellStyle   = context.Instance as DataGridViewCellStyle;
            var listControl = context.Instance as ListControl;

            Debug.Assert(
                listControl is not null || cellStyle is not null,
                "this editor is used for the DataGridViewCellStyle::Format and the ListControl::FormatString properties");

            Application.SetHighDpiMode(HighDpiMode.SystemAware);

            _formatStringDialog ??= new FormatStringDialog(context);

            if (listControl is not null)
            {
                _formatStringDialog.ListControl = listControl;
            }
            else
            {
                _formatStringDialog.DataGridViewCellStyle = cellStyle;
            }

            if (provider.TryGetService(out IComponentChangeService changeService))
            {
                if (cellStyle is not null)
                {
                    changeService.OnComponentChanging(cellStyle, TypeDescriptor.GetProperties(cellStyle)["Format"]);
                    changeService.OnComponentChanging(cellStyle, TypeDescriptor.GetProperties(cellStyle)["NullValue"]);
                    changeService.OnComponentChanging(cellStyle, TypeDescriptor.GetProperties(cellStyle)["FormatProvider"]);
                }
                else
                {
                    changeService.OnComponentChanging(listControl, TypeDescriptor.GetProperties(listControl)["FormatString"]);
                    changeService.OnComponentChanging(listControl, TypeDescriptor.GetProperties(listControl)["FormatInfo"]);
                }
            }

            editorService.ShowDialog(_formatStringDialog);
            _formatStringDialog.End();

            if (!_formatStringDialog.Dirty)
            {
                return(value);
            }

            // Since the bindings may have changed, the properties listed in the properties window need to be refreshed.
            TypeDescriptor.Refresh(context.Instance);

            if (changeService is not null)
            {
                if (cellStyle is not null)
                {
                    changeService.OnComponentChanged(cellStyle, TypeDescriptor.GetProperties(cellStyle)["Format"]);
                    changeService.OnComponentChanged(cellStyle, TypeDescriptor.GetProperties(cellStyle)["NullValue"]);
                    changeService.OnComponentChanged(cellStyle, TypeDescriptor.GetProperties(cellStyle)["FormatProvider"]);
                }
                else
                {
                    changeService.OnComponentChanged(listControl, TypeDescriptor.GetProperties(listControl)["FormatString"]);
                    changeService.OnComponentChanged(listControl, TypeDescriptor.GetProperties(listControl)["FormatInfo"]);
                }
            }

            return(value);
        }