复制粘贴在属性网格禁用
-
20-09-2019 - |
题
我有一个属性网格内的FileNameEditor,其具有几个条目等
主文件: “C:\ blah1”
秒文件: “C:\ blah2”
等。
我的问题是,我不能复制和从一个属性条目到另一个粘贴,我不能在字段中键入手动为好。是否有一个特定的属性,这将使该FileNameEditor内编辑。 实施例
public class MyEditor : FileNameEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return false;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
object e = base.EditValue(context, provider, value);
if ((value as MyFileS) != null)
{
(value as MyFilesS).FileName = (String) e;
}
return e;
}
protected override void InitializeDialog(OpenFileDialog openFileDialog)
{
base.InitializeDialog(openFileDialog);
}
}
由于
解决方案
为什么要使用自定义编辑器?如果你只想一个对象的字符串属性,然后马克Gravell的答案的作品。
然而,如果“文件”属性的属性网格内的对象是你还需要实现一个自定义类型转换器的自定义类。
例如:
namespace WindowsFormsApplication
{
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Globalization;
using System.Windows.Forms;
using System.Windows.Forms.Design;
class MyEditor : FileNameEditor
{
public override bool GetPaintValueSupported(ITypeDescriptorContext context)
{
return false;
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
string s = Environment.CurrentDirectory;
object e = base.EditValue(context, provider, value);
Environment.CurrentDirectory = s;
var myFile = value as MyFile;
if (myFile != null && e is string)
{
myFile.FileName = (string)e;
return myFile;
}
return e;
}
}
class MyFileTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if (destinationType == typeof(string))
return true;
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string)
return new MyFile { FileName = (string)value };
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
var myFile = value as MyFile;
if (myFile != null && destinationType == typeof(string))
return myFile.FileName;
return base.ConvertTo(context, culture, value, destinationType);
}
}
[TypeConverter(typeof(MyFileTypeConverter))]
[Editor(typeof(MyEditor), typeof(UITypeEditor))]
class MyFile
{
public string FileName { get; set; }
}
class MyFileContainer
{
public MyFileContainer()
{
File1 = new MyFile { FileName = "myFile1.txt" };
File2 = new MyFile { FileName = "myFile2.txt" };
}
public MyFile File1 { get; set; }
public MyFile File2 { get; set; }
}
static public class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (var f = new Form())
using (var pg = new PropertyGrid())
{
pg.Dock = DockStyle.Fill;
pg.SelectedObject = new MyFileContainer();
f.Controls.Add(pg);
Application.Run(f);
}
}
}
}
要支持编辑的文件名中的地方,还剪切和粘贴PropertyGrid中需要知道如何从一个字符串转换成你的“文件”类型,然后再返回。如果不实现在类型转换器转换成字符串的方法属性将显示对象的ToString()的结果。
我建议鞭打Reflector.Net和读取源有的在BCL的UITypeEditors和类型转换器,因为它可以是相当翔实看到微软如何支持编辑颜色,时间跨度,日期时间等财产的网格。
此外,小心打开文件对话框。如果你不小心标准的WinForms打开文件对话框可以改变你的应用程序的当前工作目录。我不认为FileNameEditor有这个问题,但我只测试了这个在Windows 7下。
其他提示
不能再现;这工作正常 - 可以在网格和弹出复制/粘贴两者(?做你的财产有一个setter):
using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
class Foo
{
[Editor(typeof(FileNameEditor), typeof(UITypeEditor))]
public string Name { get; set; }
[STAThread]
static void Main()
{
using (Form form = new Form())
using (PropertyGrid grid = new PropertyGrid())
{
grid.Dock = DockStyle.Fill;
form.Controls.Add(grid);
grid.SelectedObject = new Foo();
Application.Run(form);
}
}
}
不隶属于 StackOverflow