Propertygrid context menu

G

Guest

Hi

In the VS .Net the Propertygrid has a context menu
with 'Reset' and 'Description' menu points.

I want to have this context menu also for runtime
PropertyGrids.
How can this be done?

Sincerely
orbit
 
I

Iulian Ioenscu

You must inherit it and declare your own context menu.
Then catch the click events and show the menu. Then take
the selected property and do a reflection to grab
the "Resetxxxx" method and if found call it. This is how
i did it and it worked fine...
 
Y

Ying-Shen Yu[MSFT]

Hi Jim,

Thanks for your post!

You may create a context menu and make these two menu items by yourself.
the Description is using PropertyGrid.HelpVisible property, the Reset
command needs a bit more work,
1. get the selected property in the propertygrid from
PropertyGrid.SelectedGridItem.
2. get the property descriptor of the selected property descriptor from
GridItem.PropertyDescriptor.
3. get the DefaultValueAttribute of the PropertyDescriptor. if the property
doesn't have one, set the Reset menuitem to disabled.
4. if the DefaultValueAttribute exists, get the current value of this
property and compare it with the default value, set the correct menu state.
5. to reset the value of this property, you may use
PropertyDescriptor.ResetValue method.

Here is a sample snippet, Hope it will be helpful :
<code>
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
DefaultValueAttribute defValue =
item.PropertyDescriptor.Attributes[typeof(DefaultValueAttribute)] as
DefaultValueAttribute;
if (defValue != null && defValue.Value != null)
{
object obj =
item.PropertyDescriptor.GetValue(propertyGrid1.SelectedObject);

menuItem1.Enabled = !(obj.Equals(defValue.Value));
}
else menuItem1.Enabled = false;

menuItem2.Checked = propertyGrid1.HelpVisible;
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
propertyGrid1.HelpVisible = ! propertyGrid1.HelpVisible;
menuItem2.Checked = propertyGrid1.HelpVisible;
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
item.PropertyDescriptor.ResetValue(propertyGrid1.SelectedObject);
}
</code>

Does it solve your problem?
If you still have problem on it please be free to reply this thread.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
J

Jim di Griz

Thank you very much Ying-Shen Yu

Your "code snippet" helped me a lot.

There is only one thing.
Some of my properties do not use the
DefaultPropertyAttribute but the ResetXXXXXX method to
reset. Though I had to enhance your code for these cases.

Which one of these two methods is to be handled first if
(by accident) both are implemented?

I handle the DefaultPropertyAttribute first and invoke the
ResetXXXXXX method only if there is no
DefaultPropertyAttribute. Is this correct?

Sincerely
orbit

-----Originalnachricht-----
Hi Jim,

Thanks for your post!

You may create a context menu and make these two menu items by yourself.
the Description is using PropertyGrid.HelpVisible property, the Reset
command needs a bit more work,
1. get the selected property in the propertygrid from
PropertyGrid.SelectedGridItem.
2. get the property descriptor of the selected property descriptor from
GridItem.PropertyDescriptor.
3. get the DefaultValueAttribute of the
PropertyDescriptor. if the property
doesn't have one, set the Reset menuitem to disabled.
4. if the DefaultValueAttribute exists, get the current value of this
property and compare it with the default value, set the correct menu state.
5. to reset the value of this property, you may use
PropertyDescriptor.ResetValue method.

Here is a sample snippet, Hope it will be helpful :
<code>
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
DefaultValueAttribute defValue =
item.PropertyDescriptor.Attributes[typeof (DefaultValueAttribute)] as
DefaultValueAttribute;
if (defValue != null && defValue.Value != null)
{
object obj =
item.PropertyDescriptor.GetValue (propertyGrid1.SelectedObject);

menuItem1.Enabled = !(obj.Equals (defValue.Value));
}
else menuItem1.Enabled = false;

menuItem2.Checked = propertyGrid1.HelpVisible;
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
propertyGrid1.HelpVisible = ! propertyGrid1.HelpVisible;
menuItem2.Checked = propertyGrid1.HelpVisible;
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
item.PropertyDescriptor.ResetValue (propertyGrid1.SelectedObject);
}
</code>

Does it solve your problem?
If you still have problem on it please be free to reply this thread.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.

.
 
Y

Ying-Shen Yu[MSFT]

Hi orbit,

I'm glad to hear my snippet is helpful to you.

In your second question, which approach should apply, when both of them
were defined.
In my quick test, the VS.NET IDE uses the DefaultPropertyAttribute when
both exists.
So your current implementation should be fine.
Thanks!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 
A

Andrew \(Infragistics\)

Why not just get the PropertyDescriptor from the SelectedGridItem and call
its ShouldSerializeValue method?
 
Y

Ying-Shen Yu[MSFT]

Hi Andrew,

Yes, the ShouldSerializeValue will make things easier, I digging the
documentation again and found a more suitable method CanResetValue.
Then the code can be simplified to
<code>
private void contextMenu1_Popup(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
menuItem1.Enabled =
item.PropertyDescriptor.CanResetValue(propertyGrid1.SelectedObject);
menuItem2.Checked = propertyGrid1.HelpVisible;
}

private void menuItem2_Click(object sender, System.EventArgs e)
{
propertyGrid1.HelpVisible = ! propertyGrid1.HelpVisible;
menuItem2.Checked = propertyGrid1.HelpVisible;
}

private void menuItem1_Click(object sender, System.EventArgs e)
{
GridItem item = propertyGrid1.SelectedGridItem;
item.PropertyDescriptor.ResetValue(propertyGrid1.SelectedObject);
propertyGrid1.Refresh();
}
menuItem1.Enabled =
item.PropertyDescriptor.CanResetValue(propertyGrid1.SelectedObject);
</code>
Thank you for your suggestion, I learn some useful methods from it!

Best regards,

Ying-Shen Yu [MSFT]
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security

This posting is provided "AS IS" with no warranties and confers no rights.
This mail should not be replied directly, "online" should be removed before
sending.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top