[IronPython] System.Windows.Forms.PropertyGrid and Python objects (Solution)

Charles Mason cemasoniv at gmail.com
Thu Apr 24 16:00:49 CEST 2008


At least temporary anyway.

I've put my solution in mercurial:
http://www.chelestra.com/hg/index.py/Chuck.Custom/file/36b285715130/Chuck.Custom/

Basically, I've inherited PropertyGrid and provided a "SetPythonObject"
method (See CustomPropertyGrid).

The Private object holder has a property with custom type converter on it
that calls "GetProperties" on the given python object.  In Python, I
implement said method in its simplest case as:

class MyObject:
    field = "name"
    def GetProperties(self):
        return TypeDescriptor.GetProperties(self)

In actuality, though, I've done some filtering of properties so that I don't
get all the nice pythony stuff.

Works very nicely but I have come across a few more issues.  Most notably I
wanted the ability to nest objects like:

class MyOtherObject:
    instance = MyObject()
    def GetProperties(self): return TypeDescriptor.GetProperties(self)

This doesn't work out of the box, and is what the PublicCustomObjectHolder
is for: simply derive from it and implement GetProperties.

class MyObject(PublicCustomObjectHolder):
    field = "name"
    def GetProperties(self):
        return TypeDescriptor.GetProperties(self)

class MyOtherObject(PublicCustomObjectHolder):
    instance = MyObject()
    def GetProperties(self): return TypeDescriptor.GetProperties(self)

Calling PropertyGrid.SetPythonObject( MyOtherObject() ) now works as
desired.

Thanks to everyone for the hints.  Perhaps this can be useful to someone?

Chuck

On Wed, Apr 23, 2008 at 12:02 PM, Dino Viehland <
dinov at exchange.microsoft.com> wrote:

>  If it is that conversion issue I actually think we might be able to fix
> it.  We do have a Converter.CanConvertFrom method and I'm not sure why we're
> not calling that instead of creating an instance and calling TryConvert.  It
> should be a relatively easy fix so I've raised the priority to high but I'd
> also encourage voting on the bug.
>
>
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Charles Mason
> *Sent:* Wednesday, April 23, 2008 8:52 AM
>
> *To:* Discussion of IronPython
> *Subject:* Re: [IronPython] System.Windows.Forms.PropertyGrid and Python
> obj ects
>
>
>
> Yes.
>
> It's the only article that's worth reading that google turned up.
>
> On Wed, Apr 23, 2008 at 11:30 AM, Masters, Christopher <
> christopher.masters at credit-suisse.com> wrote:
>
> Is it related to this?
>
>
>
> http://www.codeplex.com/IronPython/WorkItem/View.aspx?WorkItemId=13405
>
>
>
>
>  ------------------------------
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Charles Mason
>
> *Sent:* 23 April 2008 16:26
> *To:* Discussion of IronPython
> *Subject:* Re: [IronPython] System.Windows.Forms.PropertyGrid and Python
> objects
>
> I should have mentioned that I am indeed using IronPython 1.1.1.
>
> >From what I can tell the objects are working very nicely with WinForms
> with the exception of PropertyGrid.
>
> Were you using Propertygrid specifically?
>
> C
>
> On Wed, Apr 23, 2008 at 11:00 AM, Pigneri, Rocco <rpigneri at lavastorm.com>
> wrote:
>
> Dear Charles,
>
>
>
> I ran into the same problem you are experiencing a few months ago while
> using Python 1.1.  The problem is that the WinForms reflection is looking
> for static properties.  IronPython 1.1 objects, being dynamic, lack the
> proper metadata to guide the WinForms classes to do the right thing.
>
>
>
> Try 1.1.1.  That will allow WinForms to find a *whole* lot of things
> (including all your public methods).  My guess is that it may be easier for
> you to turn off these extra elements rather than to create a separate C#
> assembly to hold static interfaces.
>
>
>
> It would be great to clean up these staticized properties in 1.1.2 to
> include only properties defined with the "property" function.  That would
> really help anyone using static data binding in WinForms (or any other part
> of the BCL for that matter).
>
>
>
> Hope that helps,
>
>
>
> Rocco
>
>
>  ------------------------------
>
> *From:* users-bounces at lists.ironpython.com [mailto:
> users-bounces at lists.ironpython.com] *On Behalf Of *Charles Mason
> *Sent:* Wednesday, April 23, 2008 9:31 AM
> *To:* users at lists.ironpython.com
> *Subject:* [IronPython] System.Windows.Forms.PropertyGrid and Python
> objects
>
> So I've been through hell last night tearing my hear out trying to figure
> out how to work PropertyGrid properly with a Python class object.
>
> Things I've tried:
>
> 1) In Python, PropertyGrid.SelectedObject = pythonobj
> 2) In Python, Deriving PropertyGrid and overriding CreatePropertyTab
> 3) In C#, deriving a new class from PropertyGrid and implementing
>
> public void SetPythonObject(Object obj)
> {
>   this.SelectedObject = obj;
> }
>
>
> 4) In C#, Overriding CreatePropertyTab() (I get a warning about hiding a
> baseclass implementation -- sounds like this isn't overrideable)
> 5) In C#, creating a wrapper class:
>
> class CustomHolder
> {
>   private Object heldObj = null;
>   public Object held {
>     get { return heldObj; }
>     set { heldObj = value; }
>   }
>   CustomHolder(Object obj)
>   {
>     this.held = obj;
>   }
> }
>
> and in the derived PropertyGrid class:
>
> public void SetPythonObject(Object obj)
> {
>   this.SelectedObject = new CustomHolder(obj);
> }
>
> --
>
> Only #5 so far has done anything worthwhile: In the property grid as a
> single field I get what looks like str(obj) output:
> <module.CLASSNAME instance at 0x................>
>
> I've also considered using the TypeDescriptor() class/methods to create
> each property grid entry myself, but I don't see anything at all about where
> PropertyGrid gets its property fields from -- see #4.
>
> Is there any way to do this conveniently (I'd prefer not to write a C#
> custom dll, but am amiable to the requirement of doing so)?
>
> Chuck
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
> ==============================================================================
>
> Please access the attached hyperlink for an important electronic communications disclaimer:
>
>
>
> http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
> ==============================================================================
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20080424/19ed8e74/attachment.html>


More information about the Ironpython-users mailing list