PropertyGrid does not raise PropertyValueChanged event

試著忘記壹切 提交于 2020-01-15 15:27:24

问题


When user changes text or bool value in PropertyGrid I set flagModified=true; in event handler:

private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
    {
        propertyGrid1.Refresh();
        PropertyChanged(true);
    } 

and then Save button is enabled.

I use my Editor and form (see class below) to edit one of values in Properrtygrid. It is object of my class. After object is changed in editor and editor closed I re-assign value of object to the new value (value = frm.m_DS;). All works fine but one moment: in this case PropertyValueChanged is not raised. I use PropertyValueChanged event to enable my button "Save" which saves all properties to file. How I can catch event that value is changed and enable Save button?.

public class DataProviderEditor : UITypeEditor
    {

        public override Object EditValue(
          ITypeDescriptorContext context,
          IServiceProvider provider,
          Object value)
        {
            if ((context != null) && (provider != null))
            {
                IWindowsFormsEditorService svc =
                  (IWindowsFormsEditorService)
                  provider.GetService(typeof(IWindowsFormsEditorService));

                if (svc != null)
                {
                    using (DatasourceForm frm =
                      new DatasourceForm((MyDatasource)value))
                    {
                        if (svc.ShowDialog(frm) == DialogResult.OK)
                        {
                            value = frm.m_DS;
                        }
                    }
                }
            }

            return base.EditValue(context, provider, value);
        }

回答1:


I've noticed the documentation about the event PropertyGrid.PropertyValueChanged is flawed. It doesn't mention that the event is raised only if the user changes the value. You can find this out if you check the PropertyValueChangedEventArgs documentation, which says:

The PropertyValueChanged event occurs when the user changes the value of a property, which is specified as a GridItem, in a PropertyGrid.

What you can do is to add PropertyValueChanged event to the object that is browsed by PropertyGrid. You can code it similar to this:

public class BrowsedObject
{
    public event EventHandler PropertyValueChanged;

    private void OnPropertyValueChanged(object sender, EventArgs e)
    {
        EventHandler eh = PropertyValueChanged;
        if (eh != null)
            eh(sender, e);
    }

    private string someProperty;
    public new string SomeProperty
    {
        get { return someProperty; }
        set
        {
            someProperty = value;
            OnPropertyValueChanged(this, EventArgs.Empty);
        }
    }
}

Each time a new value is assigned to SomeProperty the object will raise PropertyValueChanged. You can hook up to this event logic that is to enable Save button etc.

Or

Why don't you just call PropertyChanged(true); each time you change the object property from the code.




回答2:


For a very simple "autoupdate" staff you can do this:

propertyGrid1.SelectedObject = propertyGrid1.SelectedObject;

It's seems it worked, change is visible.



来源:https://stackoverflow.com/questions/15110594/propertygrid-does-not-raise-propertyvaluechanged-event

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!