كيف يمكنني إجبار PropertyGrid على إظهار مربع حوار مخصص لخاصية معينة؟

StackOverflow https://stackoverflow.com/questions/359690

  •  21-08-2019
  •  | 
  •  

سؤال

لدي فئة ذات خاصية سلسلة، تحتوي على كل من getter وsetter، وغالبًا ما تكون طويلة جدًا بحيث تقوم PropertyGrid باقتطاع قيمة السلسلة.كيف يمكنني إجبار PropertyGrid على إظهار علامة القطع ثم تشغيل مربع حوار يحتوي على مربع نص متعدد الأسطر لتسهيل تحرير الخاصية؟أعلم أنه ربما يتعين علي تعيين نوع ما من السمات على الخاصية، ولكن ما هي السمة وكيف؟هل يجب أن يتضمن مربع الحوار الخاص بي بعض واجهات المصمم الخاصة؟

تحديث: هذا ربما يكون هذا هو الجواب على سؤالي، لكني لم أتمكن من العثور عليه من خلال البحث.سؤالي أكثر عمومية، ويمكن استخدام إجابته لإنشاء أي نوع من المحررات المخصصة.

هل كانت مفيدة؟

المحلول

تحتاج إلى تعيين [Editor(...)] للممتلكات، مما يمنحها UITypeEditor الذي يقوم بالتحرير؛مثل ذلك (مع المحرر الخاص بك ...)

using System;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;


static class Program
{
    static void Main()
    {
        Application.Run(new Form { Controls = { new PropertyGrid { SelectedObject = new Foo() } } });
    }
}



class Foo
{
    [Editor(typeof(StringEditor), typeof(UITypeEditor))]
    public string Bar { get; set; }
}

class StringEditor : UITypeEditor
{
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        IWindowsFormsEditorService svc = (IWindowsFormsEditorService)
            provider.GetService(typeof(IWindowsFormsEditorService));
        if (svc != null)
        {
            svc.ShowDialog(new Form());
            // update etc
        }
        return value;
    }
}

قد تتمكن من تعقب محرر موجود من خلال النظر في الخصائص الموجودة التي تتصرف كما تريد.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top