This is a migrated thread and some comments may be shown as answers.

How to hide some enum value on some conditions?

1 Answer 1571 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Veteran
Steve asked on 05 Feb 2020, 01:13 PM

I have a class like,

        enum ConstraintType
            {
            Horizental,
            Vertial,
            Slope,
            Offset
            }

       class Constraint

{

ConstraintType First {get;set;}

ConstraintType Second {get;set;}

}

I know the enum will be dropdownlist on the propertygrid.

I hope the candidate enum values of Second is Slope and Offset when the First equals Vertical. 

I want to know how to display limited enum value in dropdownlist according to some other property.

Thanks.

1 Answer, 1 is accepted

Sort by
0
Accepted
Dess | Tech Support Engineer, Principal
Telerik team
answered on 07 Feb 2020, 12:56 PM
Hello, Steve,   

In order to control what items to the displayed in the drop down editor, it is necessary to handle the RadPropertyGrid.EditorInitialized event and hide the items considering the value of another field.

Please refer to the sample code snippet which result is illustrated in the attached gif file.
        public RadForm1()
        {
            InitializeComponent();

            Constraint c = new Constraint();
            c.First = ConstraintType.Vertial;
            c.Second = ConstraintType.Offset;
            this.radPropertyGrid1.SelectedObject = c;

            this.radPropertyGrid1.EditorInitialized += radPropertyGrid1_EditorInitialized;
        }

        private void radPropertyGrid1_EditorInitialized(object sender, PropertyGridItemEditorInitializedEventArgs e)
        {
            visible = ElementVisibility.Visible;
            isVisible = true;
            height = 20;
            PropertyGridDropDownListEditor editor = e.Editor as PropertyGridDropDownListEditor;
            if (editor != null)
            {
                BaseDropDownListEditorElement el = editor.EditorElement as BaseDropDownListEditorElement;
                el.SelectNextOnDoubleClick = false;
                el.DropDownSizingMode = SizingMode.RightBottom;
                Constraint c = this.radPropertyGrid1.SelectedObject as Constraint;
                el.VisualItemFormatting -= el_VisualItemFormatting;
                el.VisualItemFormatting += el_VisualItemFormatting;
                if (e.Item.Label == "Second")
                {
                    if (c.First == ConstraintType.Vertial)
                    {
                        visible = ElementVisibility.Collapsed;
                        isVisible = false;
                        height = 0;
                    }
                    el.SelectedIndex = GetIndexByValue(((PropertyGridItem)e.Item).Value, el.DataSource);
                }
            }
        }

        private int GetIndexByValue(object value, object dataSource)
        {
            List<string> items = dataSource as List<string>;
            for (int i = 0; i < items.Count; i++)
            {
                if (items[i].Equals(value.ToString()))
                {
                    return i;
                }
            }
            return -1;
        }

        public ElementVisibility visible;
        public bool isVisible;
        public int height;

        private void el_VisualItemFormatting(object sender, VisualItemFormattingEventArgs args)
        {
            if (!args.VisualItem.Text.Equals(ConstraintType.Slope.ToString()) &&
                !args.VisualItem.Text.Equals(ConstraintType.Offset.ToString()))
            {
                args.VisualItem.Visibility = visible;
                args.VisualItem.Enabled = isVisible;
                args.VisualItem.Data.Height = height;
            }
            else
            {
                args.VisualItem.Visibility = ElementVisibility.Visible;
                args.VisualItem.Enabled = true;
                args.VisualItem.Data.Height = 20;
            }
        }

        enum ConstraintType
        {
            Horizental,
            Vertial,
            Slope,
            Offset
        }

        class Constraint
        {
            public ConstraintType First { get; set; }

            public ConstraintType Second { get; set; }
        }
I hope this information helps. If you need any further assistance please don't hesitate to contact me. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
PropertyGrid
Asked by
Steve
Top achievements
Rank 1
Veteran
Answers by
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or