问题
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