質問

私は

のようないくつかのエントリを持つプロパティグリッド、内部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のを切り取って貼り付けるには、あなたの「ファイル」タイプと再びに文字列から変換する方法を知っている必要があります。あなたがのTypeConverter内の文字列メソッドへの変換を実装していない場合はプロパティは、オブジェクトのToStringの結果が表示されます()。

Microsoftがプロパティグリッドにカラー、のTimeSpan、DateTimeのなどの編集をサポートしているかを確認することは非常に有益なことができるように

私はReflector.Netを泡立ておよびBCLでUITypeEditorsとTypeConvertersの一部にソースを読んでお勧めます。

また、慎重なオープンファイルダイアログなります。あなたが注意しないなら、標準の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