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?
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
}