C# - Показать и скрыть свойство динамически - PullRequest
0 голосов
/ 05 марта 2020

Я хочу показать и скрыть свойство в зависимости от другого свойства. В net есть несколько примеров, я знаю это, и я все еще гуглял это - но мой код не работает, поэтому я хочу попросить некоторую помощь.

Свойство GCSubType должно просматриваться только в том случае, если свойство GCType имеет значение SAPCustomSettings.GuiComponentType.GuiShell В зависимости от этого примера VB я попытался заставить его работать в моем случае. При изменении значения ничего не происходит - свойство GCSubType никогда не отображается. Вот мой код:

public class GetAllComponents : System.Activities.CodeActivity
        {
            private static System.ComponentModel.BrowsableAttribute _GCSubBrowsable = new System.ComponentModel.BrowsableAttribute(true);
            private SAPCustomSettings.GuiComponentType _GCType = SAPCustomSettings.GuiComponentType.GuiComponent;

            [System.ComponentModel.Category("Input"), System.ComponentModel.Description("Specify type to be returned"),
                System.ComponentModel.DisplayName("Component Type"), System.Activities.RequiredArgument, /*System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced),*/
                System.ComponentModel.Browsable(true), System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All)]

            public SAPCustomSettings.GuiComponentType GCType {
                get { return _GCType; } 
                set {
                    _GCType = value;

                    System.ComponentModel.PropertyDescriptor pDesc = null;
                    pDesc = System.ComponentModel.TypeDescriptor.GetProperties(this.GetType()).Find("GCSubType", false);
                    if (pDesc == null) { throw new System.Exception("Error - Unable to find property called 'GCSubType'"); }

                    System.ComponentModel.BrowsableAttribute BAtt = null;
                    BAtt = pDesc.Attributes[new System.ComponentModel.BrowsableAttribute(_GCType == SAPCustomSettings.GuiComponentType.GuiShell).GetType()] as System.ComponentModel.BrowsableAttribute;
                    if (BAtt == null) { throw new System.NullReferenceException("Error - BrowsableAttribute not referenced!"); }

                    System.Reflection.FieldInfo FoAtc = null;
                    FoAtc = new System.ComponentModel.BrowsableAttribute(_GCType == SAPCustomSettings.GuiComponentType.GuiShell).GetType().GetField("browsable", (System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance));
                    if (FoAtc == null) { throw new System.NullReferenceException("Error - FieldInfo is not referenced!"); }

                    FoAtc.SetValue(BAtt, _GCType == SAPCustomSettings.GuiComponentType.GuiShell);
                } 
            }

            [System.ComponentModel.Category("Input"), System.ComponentModel.Description("Specify subtype of GuiShell"),
                System.ComponentModel.DisplayName("GuiShell SubType"), System.ComponentModel.Browsable(false), 
                System.ComponentModel.RefreshProperties(System.ComponentModel.RefreshProperties.All)]

            public SAPCustomSettings.GuiComponentSubType GCSubType { get; set; }
            protected override void Execute(System.Activities.CodeActivityContext context)
            {
                throw new System.NotImplementedException();
            }
        }
...