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

Show or hide some property at runtime.

1 Answer 63 Views
PropertyGrid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Veteran
Steve asked on 14 Jan 2020, 08:09 AM

I have a customize object just like this.

01.public class Person
02.        {
03.        [CategoryAttribute("Personal Info.")]
04.        public Name Name { get; set; }
05.        [CategoryAttribute("Personal Info.")]
06.        public int Age { get; set; }
07.       [CategoryAttribute("Profession Info.")]
08.        public Profession Profession { get; set; }
09.        }
10. 
11.      public class Profession
12.        {
13.        private Title title_;
14. 
15.        public string Company { get; set; }
16.        public string Address { get; set; }
17.        public Title Title
18.            {
19.            get { return title_; }
20.            set
21.                {
22.                title_ = value;
23.                if (title_ == Title.Manager)
24.                    {
25.// hide property subordinates
26.                    }
27.                else if (title_ == Title.Developer)
28.                    {
29.// show property subordinates
30.                    }
31.                }
32.            }
33. 
34.        public List<Person> Subordinate { get; set; }
35.        }

 

Is there a way to show or hide Subordinate according to the property Title? When I selected the Title with Manager, the subordinate will appear. When I selected the Title with Developer, the subordinate will hide.

1 Answer, 1 is accepted

Sort by
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 16 Jan 2020, 12:51 PM

Hello, Steve,    

Following the provided code snippet, I have prepared a sample project which result is illustrated below:

Here is the full code snippet for producing this output: 

        public RadForm1()
        {
            InitializeComponent();
            
            this.radPropertyGrid1.ItemFormatting += radPropertyGrid1_ItemFormatting;

            Person employee1 = new Person();
            employee1.Name = new Name("Ana", "Brown");
            employee1.Age = 28;
            employee1.Profession = new Profession() { Company = "Progress", Title = Title.Developer, Address = "Sofia" };

            Person employee2 = new Person();
            employee2.Name = new Name("Ana", "Brown");
            employee2.Age = 28;
            employee2.Profession = new Profession() { Company = "Progress", Title = Title.Developer, Address = "Sofia" };

            Person person = new Person();
            person.Name = new Name("John", "Smith");
            person.Age = 36;
            person.Profession = new Profession() { Company = "Progress", Title = Title.Manager, Address = "Sofia", Subordinate = new List<Person> { employee1, employee2 } };

            this.radPropertyGrid1.SelectedObject = person;
        }

        private void radPropertyGrid1_ItemFormatting(object sender, PropertyGridItemFormattingEventArgs e)
        {
            if (e.Item.Label == "Name" || e.Item.Label == "Profession")
            {
                ((PropertyGridItemElement)e.VisualElement).ValueElement.Text = "";
            }
        }

        public class Person
        {
            [CategoryAttribute("Personal Info.")]
            [TypeConverter(typeof(ExpandableObjectConverter))]
            public Name Name { get; set; }

            [CategoryAttribute("Personal Info.")]
            public int Age { get; set; }

            [CategoryAttribute("Profession Info.")]
            [TypeConverter(typeof(ExpandableObjectConverter))]
            public Profession Profession { get; set; }
        }

        public class Name
        {
            public string First { get; set; }

            public string Last { get; set; }

            public Name(string first, string last)
            {
                this.First = first;
                this.Last = last;
            }
        }

        public enum Title
        {
            Manager,
            Developer
        }

        public class Profession
        {
            private Title title_;

            public string Company { get; set; }

            public string Address { get; set; }

            public Title Title
            {
                get
                {
                    return title_;
                }
                set
                {
                    title_ = value;
                    if (title_ == Title.Manager)
                    {
                        // hide property subordinates
                    }
                    else if (title_ == Title.Developer)
                    {
                        // show property subordinates
                    }
                }
            }

            public List<Person> Subordinate { get; set; }
        }

If I understand your requirement correctly, you need to hide the Subordinate property if you change the Title from Manager to Developer. For this purpose you can handle the Edited event and manipulate the PropertyGridItem.Visible property as follows. The attached gif file illustrates the achieved behavior:

        private void radPropertyGrid1_Edited(object sender, PropertyGridItemEditedEventArgs e)
        {
            if (e.Item.Label=="Title")
            {
                PropertyGridItem item = e.Item as PropertyGridItem;
                if ((Title)item.Value == Title.Developer)
                {
                    item.Parent.GridItems["Subordinate"].Visible = false;

                }
                else
                {
                    item.Parent.GridItems["Subordinate"].Visible = true;
                }
            }
        }

I hope this information helps.

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