문제

속성 그리드 내부에 filenameeditor가 있습니다.

메인 파일 : "C : blah1"

SEC 파일 : "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);
    }
}

감사

도움이 되었습니까?

해결책

사용자 정의 편집기를 사용하는 이유는 무엇입니까? 객체에 문자열 속성을 원한다면 Marc 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를 자르고 붙여 넣으려면 문자열에서 "파일"유형으로 변환하는 방법을 알아야합니다. typeconverter에서 변환으로 문자열 메서드를 구현하지 않으면 속성은 객체의 toString ()의 결과를 표시합니다.

BCL의 일부 UityPeeditors 및 TypeConverters에 대해 Reflector.net을 채찍질하고 소스를 읽는 것이 좋습니다. Microsoft가 속성 그리드의 편집 색상, TimesPan, DateTime 등을 어떻게 지원하는지 확인하는 것이 매우 유익합니다.

또한 파일 개방 대화 상자를 조심하십시오. 표준 winforms 열린 파일 대화 상자는 조심하지 않으면 응용 프로그램 현재 작업 디렉토리를 변경할 수 있습니다. FilenameEditor 에이 문제가 있다고 생각하지는 않지만 Windows 7에서만 테스트했습니다.

다른 팁

재현 할 수 없습니다. 이것은 잘 작동합니다. 그리드와 팝업에 모두 복사/붙여 넣을 수 있습니다 (속성에 세터가 있습니까?) :

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);
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top