Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.

PropertyGrid ValueChanged event

[es] :: .NET :: .NET Desktop razvoj :: PropertyGrid ValueChanged event

[ Pregleda: 1831 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Autor

Pretraga teme: Traži
Markiranje Štampanje RSS

DarkMan
Darko Matesic

Član broj: 20445
Poruke: 572
77.46.226.*

Jabber: DarkMan


Profil

icon PropertyGrid ValueChanged event25.06.2008. u 15:51 - pre 192 meseci
U PropertyGrid-u imam prikazan property klasu MyValue. Ova klasa ima jedan bool property Status.
Vrednost property-a ove klase se mogu menjati na dva nacina preko ExpandableObjectConverter-a ili UITypeEditor-a.
Kada se vrednost menja na prvi nacin (klikne se na + znak sa leve strane i direkno menja true/false za property Status) dogadjaj PropertyValueChanged ce biti izvrsen.
Ako se vrednost menja na drugi nacin (na DropDown dugme sa desne strane) vrednost ce biti promenjena ali se dogadjaj PropertyValueChanged nece izvrsiti.

Code:

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace WindowsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }

    public class Form1: Form
    {
        public Form1()
        {
            PropertyGrid grid = new PropertyGrid();
            grid.Dock = DockStyle.Fill;
            grid.SelectedObject = new MyObject();
            grid.PropertyValueChanged += new PropertyValueChangedEventHandler(grid_PropertyValueChanged);
            this.Controls.Add(grid);
            this.Size = new Size(500, 200);
        }

        private void grid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
        {
            this.Text = "PropertyValueChanged: " + e.ChangedItem.Label + " = " + e.ChangedItem.Value;
        }
    }

    [TypeConverter(typeof(MyValueConverter))]
    [Editor(typeof(MyValueEditor), typeof(UITypeEditor))]
    public class MyValue
    {
        private bool m_Status = false;
        public bool Status
        {
            get { return m_Status; }
            set { m_Status = value; }
        }
    }

    public class MyValueConverter: ExpandableObjectConverter
    {
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destType)
        {
            if(destType == typeof(string) && value is MyValue) {
                MyValue p = (MyValue)value;
                return "Status = " + p.Status;
            }
            return base.ConvertTo(context, culture, value, destType);
        }
    }

    public class MyObject
    {
        private MyValue m_Value = new MyValue();
        public MyValue Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
    }

    public class MyValueEditor: UITypeEditor
    {
        private MyValueEditorUI editorUI = null;

        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if(provider != null) {
                IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if(editorService == null) {
                    return value;
                }
                if(this.editorUI == null) {
                    this.editorUI = new MyValueEditorUI();
                }
                this.editorUI.Start(editorService, value);
                editorService.DropDownControl(this.editorUI);
                this.editorUI.End();
            }
            return value;
        }

    }

    public class MyValueEditorUI: Control
    {
        private IWindowsFormsEditorService editorService = null;
        private object value = null;
        private bool wait = false;

        private CheckBox checkStatus;

        public MyValueEditorUI()
        {
            checkStatus = new CheckBox();
            checkStatus.Text = "Status";
            checkStatus.CheckedChanged += new EventHandler(checkStatus_CheckedChanged);
            this.Controls.Add(checkStatus);
            this.Size = new Size(50, 50);
        }

        private void checkStatus_CheckedChanged(object sender, EventArgs e)
        {
            UpdateValue();
            CloseUI();
        }

        private void CloseUI()
        {
            if(this.editorService != null) {
                this.editorService.CloseDropDown();
            }
        }

        public object Value
        {
            get { return this.value; }
        }


        public void Start(IWindowsFormsEditorService editorService, object value)
        {
            this.editorService = editorService;
            this.value = value;
            UpdateControl();
        }

        public void End()
        {
            this.editorService = null;
            this.value = null;
        }

        public void UpdateValue()
        {
            if(wait) return;
            MyValue value = this.value as MyValue;
            if(value != null) {
                value.Status = checkStatus.Checked;
            }
        }

        public void UpdateControl()
        {
            MyValue value = this.value as MyValue;
            if(value != null) {
                wait = true;
                checkStatus.Checked = value.Status;
                wait = false;
            }
        }

    }
}


Kako obavestiti PropertyGrid da je vrednost promenjena?
 
Odgovor na temu

DarkMan
Darko Matesic

Član broj: 20445
Poruke: 572
93.86.80.*

Jabber: DarkMan


Profil

icon Re: PropertyGrid ValueChanged event30.06.2008. u 15:56 - pre 192 meseci
Isti problem sam imao i ranije kada sam pravio svoj CollectionEditor, tj. nisam mogao da obavestim PropertyGrid o promeni vrednosti.
Izgleda da ne postoji "regularan" nacin osim koriscenja refleksije i rucnog pozivanja eventa.
Parametar ITypeDescriptorContext context metode EditValue ima referencu na PropertyGrid koja je dostupna samo preko refleksije. Kada dodjemo do PropertyGrid-a refleksijom dolazimo do PropertyValueChanged i rucno ga pozivamo.

 
Odgovor na temu

[es] :: .NET :: .NET Desktop razvoj :: PropertyGrid ValueChanged event

[ Pregleda: 1831 | Odgovora: 1 ] > FB > Twit

Postavi temu Odgovori

Navigacija
Lista poslednjih: 16, 32, 64, 128 poruka.