수정하는 방법 PropertyGrid 런타임(추가/제거를 제공하고 역동적 형/enum)

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

문제

당신은 어떻게 수정할 수 propertygrid 런타임 시에 모든 방법은?할 수 있는 추가 및 제거 속성을 추가"동적 유형",와 함께 무엇을 의미하는 입력한 결과에서 생성된 런타임에서 드롭다운 메뉴를 propertygrid 를 사용하여 형식 변환기 지원.

나는 실제로 모두 할 수 있는 것들(추가/제거 속성을 추가 dynamic 입력)하지만 별도로지에서 동일한 시간입니다.

을 구현을 지원하는 추가 및 제거 속에서 런타임이 내가 사용하는 이 codeproject 문서 고 코드를 수정하는 조금 다른 유형을 지원하지 않음(문자열).

private System.Windows.Forms.PropertyGrid propertyGrid1;
private CustomClass myProperties = new CustomClass();

public Form1()
{
    InitializeComponent();

    myProperties.Add(new CustomProperty("Name", "Sven", typeof(string), false, true));
    myProperties.Add(new CustomProperty("MyBool", "True", typeof(bool), false, true));
    myProperties.Add(new CustomProperty("CaptionPosition", "Top", typeof(CaptionPosition), false, true));
    myProperties.Add(new CustomProperty("Custom", "", typeof(StatesList), false, true)); //<-- doesn't work
}

/// <summary>
/// CustomClass (Which is binding to property grid)
/// </summary>
public class CustomClass: CollectionBase,ICustomTypeDescriptor
{
    /// <summary>
    /// Add CustomProperty to Collectionbase List
    /// </summary>
    /// <param name="Value"></param>
    public void Add(CustomProperty Value)
    {
        base.List.Add(Value);
    }

    /// <summary>
    /// Remove item from List
    /// </summary>
    /// <param name="Name"></param>
    public void Remove(string Name)
    {
        foreach(CustomProperty prop in base.List)
        {
            if(prop.Name == Name)
            {
                base.List.Remove(prop);
                return;
            }
        }
    }

등...

public enum CaptionPosition
{
    Top,
    Left
}

나의 완벽한 솔루션이 될 수 있습 다운로드 .

그것은 잘 작동할 때 나는 문자열 추가,bools 또는 열거하지만,추가하여 빠르게 액세스할 수 있습니"동적 유형을"좋아 StatesList 그것은 작동하지 않습니다.누군가는 이유를 알고 나를 도울 수 있습니 그것을 해결하기 위해시겠습니까?

public class StatesList : System.ComponentModel.StringConverter
{
    private string[] _States = { "Alabama", "Alaska", "Arizona", "Arkansas" };

    public override System.ComponentModel.TypeConverter.StandardValuesCollection
    GetStandardValues(ITypeDescriptorContext context)
    {
        return new StandardValuesCollection(_States);
    }

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
    {
        return true;
    }
}

의 방법을 사용하여 형식 변환기 지원 작동하지 않을 때에도 추가 숙박 시설에 런타임 예를 들어, 이 코드 문제없이 작동하지만,나는 할 수 있다.

에서 봐 주시기 바랍니다 내 프로젝트.감사합니다!

도움이 되었습니까?

해결책

당신이하는 일은 Stateslist (타입 콘버터)를 속성으로 추가하는 것입니다.
당신이해야 할 일은 Stateslist가있는 속성을 타이프 컨버터로 추가하는 것입니다.

다른 팁

아, 물론!

myProperties.Add(new CustomProperty("Custom", "", typeof(States), false, true));

[TypeConverter(typeof(StatesList))]
public class States
{
}

매력처럼 작동합니다. 감사합니다!

내 프로젝트를 업데이트했는데 다른 사람들에게 도움이 될 수 있기를 바랍니다. 여기.

이 질문과 답변이었의 위대한 유용하다.그러나 내가 필요로 확장하는 것을 조금 추가하여 허용한 실시간 생성되는 드롭 다운 목록 값입니다.나는 생각이 나는 것이트에서 샘플 코드에 관하여 그것은 필요한 경우에는,사람이 그것을 발견 유용합니다.

첫째,추가 옵션 매개 변수를 CustomProperty 생성자를 추가 옵션 속성:

    private List<string> lOptions;

    public CustomProperty(string sName, object value, Type tType, bool bReadOnly, bool bVisible, List<string> lOptions)
    {
        this.lOptions = lOptions;
    }

    public List<string> Options
    {
        get { return lOptions; }
    }

둘째,추가 옵션에 속성을 CustomPropertyDescriptor 클래스:

    public List<string> Options
    {
        get
        {
            return m_Property.Options;
        }
    }

셋째,나를 수정하 getstandardvalues 에 방법에 동적인 내 입력 클래스(예:StatesList)를 이용하는 새로운 옵션 숙박 시설에 CustomPropertyDescriptor 개체:

    public override StandardValuesCollection
                 GetStandardValues(ITypeDescriptorContext context)
    {
        CustomPropertyDescriptor descriptor = (CustomPropertyDescriptor)context.PropertyDescriptor;
        return new StandardValuesCollection(descriptor.Options);
    }

마지막으로,이를 통과했던 내 옵션 목록을 작성할 때 새로운 CustomProperty 개체:

    List<string> optionsList = new List<string>(new string[] { "test1", "test2", "test3" });        
    CustomProperty myProperty = new CustomProperty(attr.Name, attr.Value, valueType, false, true, optionsList);

의 장소에서 정체되는 전달에서 이 예를 들어,당신은 당신을 생성할 수 있 목록에 대한 옵션에서 드롭다운 메뉴를 어떤 방식으로 당신을 제공,당신은 완벽하게 제어 옵션을 사용할 수 있습니다.

제 경우에는 TypeConverter가 States Class에 적용되지 않았습니다.

[TypeConverter(typeof(StatesList))] // not work
public class States
{
}

그래서 CustomPropertyDescriptor에 재정의를 추가했습니다

public override TypeConverter Converter
{
    get {
        if (this.PropertyType.Equals(typeof(States)) ) {
            return new StatesList(); ; 
        }
        return base.Converter;
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top