WinForms PropertyGrid behaving unexpectedly in WPF app

♀尐吖头ヾ 提交于 2019-12-13 05:17:17

问题


I'm using the WinForms PropertyGrid to edit object properties in a WPF app. However, the PropertyGrid isn't working as expected with certain property types. Specifically, objects exposing properties of type Color or Brush are displayed as text, #FF000000, for example, and the drop-down color selector list doesn't appear when a cell of that type is clicked in the PropertyGrid. It is my understanding that built-in editors exists for these types and I shouldn't have to do anything extra to get this to work (please correct me if I'm wrong).

For sake of brevity, I won't include a gazillion lines of code, but suffice to say that everything is pretty boilerplate. My implementation is culled straight from MSDN, SO or the myriad of WPF PropertyGrid examples floating around on the web. The PropertyGrid is wrapped in a WindowsFormsHost which goes into a Window that pops up whenever a user wants to edit an object. Something akin to this:

`<Window>
    <Grid>
        <WindowsFormsHost>
            <PropertyGrid>
              ...
            </PropertyGrid>
        </WindowsFormsHost>
    </Grid>
</Window>
// BTW everything's implemented in code.

The actual objects are of course not passed to the PropertyGrid directly, but wrapped in custom PropertyEditorObjects which expose only those properties of interest in a predefined way, and at times these PropertyEditorObjects may implement an ICustomTypeDescriptor, especially when the objects being edited have child objects whose properties also need to be exposed to the PropertyGrid as nested properties. Generally:

public class PropertyEditorObject
{
    ...

    property bool SomeProperty { get; set; }
    property Size AnotherProperty { get; set; }
    property Brush YetAnotherProperty { get; set; }

    [TypeConverter(typeof(ExpandableObjectConverter))]
    property ContentProperties MoreProperties { get; }
};

public class ContentProperties : ICustomTypeDescriptor
{
    // Implements all the usual ICustomTypeDescriptor stuff.
}
// The PropertyGrid.SelectedObject is set to an instance of the PropertyEditorObject.

Everything works with the exception of what I mentioned at the start of this post: that certain property types (Colors, Fonts, etc.) are displayed as text only and the PropertyGrid doesn't use the proper Editor for that type.

The reason I'm asking fro help with this is because I'd like to use the built-in WinForms editors, such as the ColorDialog, FontDialog, etc. to edit these types without having to reinvent the wheel as my app is only in the demo stages. Any direction on this matter would be greatly appreciated. Thanx.


回答1:


This is to be expected. The WinForms property grid knows nothing of WPF types like Color, Brush, or FontFamily, so it has no custom editors for them. Have you tried one of the third-party WPF property grids? I believe the Extended WPF Toolkit has one.




回答2:


My temporary solution: Install Extended WPF Toolkit Plus, use the PropertyGrid Plus (which has a SelectedObject*s* property) as the editor, wait for the 'fix' in v2.3 that will allegedly support more 'types' (Shapes, spinning squigglies with flames shooting out the sides, the letter 'a', etc.) and, in the meantime if a user attempts to edit an unsupported 'type', throw an exception stating sorry, but you should really send us some money first. That's all for now. More sarcasm available as soon as I invent it. Thanx to the commenters for their suggestions. Toodles.




回答3:


Update on solutions:

1) Use the Winforms PropertyGrid, as stated in the original post, add EditorAttributes to properties that need them, e.g. Colors, Fonts, etc., and open the Winforms ColorDialog, FontDialog, etc., to edit them. Requires some type conversion but only a few lines of code. Not the best solution, but WORKS.

2) Use the Extended WPF Toolkit PropertyGrid. Works, sorta, but with limitations. Doesn't support all types nor multiple-object selection, i.e. no SelectedObjects property. The first issue is to be fixed in v2.3 of the Toolkit. The second is sort of a showstopper without writing wrappers to facilitate the functionality.

3) Use the Extended WPF Toolkit Plus, PropertyGrid Plus. Allows for multiple-selection, but still suffers from limited type selection. Also, costs big dinero.

4) Use the WWF PropertyInspector as a WPF PropertyGrid. Looked good on paper, but wound up functioning no better than the WinForms PropertyGrid. Requires editors/converters.



来源:https://stackoverflow.com/questions/26043815/winforms-propertygrid-behaving-unexpectedly-in-wpf-app

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