我想为我的组件的属性创建一个自定义属性编辑器。为此,我在DemoControls项目(WinForms控件库,.NET 6)中创建了一个组件和一个属性编辑器:
public partial class UserControl1 : UserControl
{
[Editor(typeof(CustomKeyEditor), typeof(UITypeEditor))]
public string CustomKey { get; set; }
public UserControl1()
{
InitializeComponent();
}
}
public class CustomKeyEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
IWindowsFormsEditorService service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
//...
return "12";
}
}
之后,我将DemoControls项目添加到Demo项目引用中,并将UserControl1组件添加到Form1中,如图所示:
但是,CustomKeyEditor不在编辑模式下工作,而是在运行时模式下工作。
然后我找到了这个例子并构建:
https://learn.microsoft.com/en-us/dotnet/api/system.drawing.design.uitypeeditor?view=windowsdesktop-6.0
但这个例子也不起作用。
对于所有的项目,我都使用了.NET6。
如果我使用.NET Framework 4.8,我的所有代码都能正常工作:
附加信息:
OS: Windows 11 Pro (21H2)
Microsoft Visual Studio Community 2022
Version 17.3.5
VisualStudio.17.Release/17.3.5+32922.545
Microsoft .NET Framework
Version 4.8.04161
Installed Version: Community
ASP.NET and Web Tools 17.3.376.3011
ASP.NET and Web Tools
Azure App Service Tools v3.0.0 17.3.376.3011
Azure App Service Tools v3.0.0
C# Tools 4.3.0-3.22470.13+80a8ce8d5fdb9ceda4101e2acb8e8eb7be4ebcea
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Common Azure Tools 1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.
Cookiecutter 17.0.22179.3
Provides tools for finding, instantiating and customizing templates in cookiecutter format.
Microsoft JVM Debugger 1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines
NuGet Package Manager 6.3.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/
Python - Profiling support 17.0.22179.3
Profiling support for Python projects.
Python with Pylance 17.0.22179.3
Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.
TypeScript Tools 17.0.10701.2001
TypeScript Tools for Microsoft Visual Studio
Visual Basic Tools 4.3.0-3.22470.13+80a8ce8d5fdb9ceda4101e2acb8e8eb7be4ebcea
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.
Visual F# Tools 17.1.0-beta.22363.4+1b94f89d4d1f41f20f9be73c76f4b229d4e49078
Microsoft Visual F# Tools
Visual Studio IntelliCode 2.2
AI-assisted development for Visual Studio.