最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

propertygrid - WinForms .NET 9: cannot find UITypeEditor - Stack Overflow

programmeradmin1浏览0评论

I have used OpenFileDialog in PropertyGrid in .NET Framework. It worked fine:

public class FileSelectorTypeEditor : System.Drawing.Design.UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        if (context == null || context.Instance == null)
            return base.GetEditStyle(context);

        return UITypeEditorEditStyle.Modal;
    }
    // etc
}

Now in WinForms on .NET 9, I cannot find the type System.Drawing.Design.UITypeEditor.

What am I doing wrong?

I have used OpenFileDialog in PropertyGrid in .NET Framework. It worked fine:

public class FileSelectorTypeEditor : System.Drawing.Design.UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        if (context == null || context.Instance == null)
            return base.GetEditStyle(context);

        return UITypeEditorEditStyle.Modal;
    }
    // etc
}

Now in WinForms on .NET 9, I cannot find the type System.Drawing.Design.UITypeEditor.

What am I doing wrong?

Share Improve this question edited Feb 6 at 18:20 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked Feb 6 at 14:06 ZedZipZedZip 6,47017 gold badges82 silver badges148 bronze badges 3
  • Works just fine on my machine, VS2022 version 17.12.0, .NET9 version 9.0.0. Hard to imagine how this could fail, given that the types are defined in the System.Windows.Forms assembly. You'd have to forget to target Winforms at all. – Hans Passant Commented Feb 6 at 15:08
  • I do not understand why it is. May be : I have an WinForms app and it depends on project library, The PropertyGrid is in app but class which is used in PropertyGrid is in the library. But how to get this type in the library? – ZedZip Commented Feb 6 at 16:11
  • Works fine for me. You should share a simple reproducing project, at least the csproj – Simon Mourier Commented Feb 6 at 18:11
Add a comment  | 

1 Answer 1

Reset to default 1

It appears you have created this Library using the generic, cross-platform Class Library template.
This template of course doesn't include the System.Windows.Forms assembly(ies).
You could have picked the Windows Forms Class Library instead.

Anyway, you can edit the Project's configuration file, and add to the main <PropertyGroup>:

<UseWindowsForms>true</UseWindowsForms>

At this point, when you try to build the Solution, you're also notified that, to include these Windows Desktop assemblies, you also need to retarget the framework definition, changing it into:

<TargetFramework>net9.0-windows</TargetFramework>

so the Project's configuration file will look like:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net9.0-windows</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>

</Project>

Of course, you could also add <UseWPF>true</UseWPF>, in case some PresentationFramework assemblies are needed in this Project.

The Library is now Windows only, and cannot be otherwise, since Windows Forms / Windows Presentation Framework (WPF) assemblies are included.

As a consequence, the class is going to change to something like this (at least in relation to the section that's been posted here):

using System.ComponentModel;
using System.Drawing.Design;

public class FileSelectorTypeEditor : UITypeEditor {
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context) {
        if (context == null || context.Instance == null)
            return base.GetEditStyle(context);

        return UITypeEditorEditStyle.Modal;
    }
    // etc
}
发布评论

评论列表(0)

  1. 暂无评论