-1

I am using WPF checkListBox And I want to populate its elements with all properties of the class given below. And I have a class named Person

namespace MyProject
{
    public class Person
    {
        public enum PersonFields
        {
            PersonPermission1,
            PersonPermission2,
        }

        bool _personPermission1;
        bool _personPermission2;

        public bool PersonPermission1
        {
            get
            { 
                return _personPermission1; 
            }
            set
            {
                if (_personPermission1!= value)
                {
                    _personPermission1= value;
                }
            }
        }

        public bool PersonPermission2
        {
            get
            {
                return _personPermission1; 
            }
            set
            {
                if (_personPermission2!= value)
                {
                    _personPermission2= value;
                }
            }
        }
    }
}

I want to populate a checkListBox with its Properties dynamically. as in given image. CheckListBox with dynamic properties

Waqas Shabbir
  • 755
  • 1
  • 14
  • 34
  • 1
    This may help get you there: http://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class – jwatts1980 Feb 09 '14 at 07:15
  • Any specific reason you have to do it this way? more common approach is to have only one property that hold values of both `PersonPermission1` & `PersonPermission2`. Then you can bind the ItemsSource to that property – har07 Feb 09 '14 at 07:18
  • I do this because I want to set permissions of different persons. – Waqas Shabbir Feb 09 '14 at 07:26
  • You can do that by binding `ItemsSource` with `Converter` and using `Reflection` to get all properties. But for checkbox binding to work twoWay, you need to have reference to actual property. If you are sure Person class will have only these two properties, why not to bind it directly with that property? – Rohit Vats Feb 09 '14 at 07:51
  • Thanx @RohitVats, but I got my answer. – Waqas Shabbir Feb 09 '14 at 07:53
  • So you want only property Names to be shown and not there actual values. Then reflection is a way to go. But like I mentioned, it won't work in TwoWay binding. Anyhow, that might not be your need for now. – Rohit Vats Feb 09 '14 at 07:55

2 Answers2

1

I you really want to get the names of all your properties, you can get a list of them like this:

typeof(Person).GetTypeInfo().DeclaredProperties.Select(prop => prop.Name).ToList(),

I noticed you also have a matching inner enum, so you could use its values instead:

Enum.GetNames(typeof(Person.PersonFields));

In both cases you'll still need additional code to set property values based on user actions.

I think a better approach would be to have a Dictionary of permissions:

var personPermissions = new Dictionary<Person.PersonFields, bool>
{
    { Person.PersonFields.PersonPermission1, false },
    { Person.PersonFields.PersonPermission2, false }
}

Now you could bind the Dictionary to ItemsSource, display Key and bind Value to checkbox.

Damir Arh
  • 17,637
  • 2
  • 45
  • 83
0

Yeah I got my answer...

chkListBoxPerson.ItemsSource = typeof(Person).GetProperties();
chkListBoxPerson.DisplayMemberPath = "Name";

Here chkListBoxPerson is the name of my CheckListBox.

Waqas Shabbir
  • 755
  • 1
  • 14
  • 34