Click here to Skip to main content
15,884,838 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am working on a project where I need to dynamically add values to a property of an object of type ProgrammerDevice .
I am using C#, WPF.

The code I have now is which I got from:
[^]

In my object class, I have the following property.
C#
private string bar;
        [TypeConverter(typeof(BarConverter))]
        [Category("Settings"), PropertyOrder(20)]
        [ReadOnly(false)]
        [DisplayName("TestProtocol")]
        public string Bar { get { return bar; } set { bar = value; OnPropertyChanged("Bar"); } }


Then in the same namespace, I have added the following:
C#
class BarConverter : StringConverter // or TypeConverter, etc
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            // is it a drop-down?
            return true;
        }
        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            // can the user *only* use the values we provide, or type their own?
            return true;
        }
        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            // this list should be the *actual* type we want to store,
            // not necessarily strings;
            // each will be converted to string when displayed
            List<string> list = new List<string>();
            list.Add("abc");
            list.Add("def");
            list.Add("ghi");

            if (context != null && context.Instance != null &&
            context.PropertyDescriptor != null)
            {
                // show how to look at the current property/value
                // : add the property name and current value (why not)
                list.Add(context.PropertyDescriptor.Name);

                list.Add((string)context.PropertyDescriptor.GetValue(context.Instance));
            }
            return new StandardValuesCollection(list);
        }
    }


copy exact from the link above.
The main code where the inputs need to come from is the following:
C#
private void GetProgrammerProtocols(ProgrammerDevice d, byte caps)
{
   List<string> Protocol_list = new List<string>();
    //Supported Protocols
    if ((caps & (byte)enumInterfaces.SWD) != 0)
    {
        d.isSWD = true;
        Protocol_list.Add("SWD");
    }
    else
    {
        d.isSWD = false;
    }
    if ((caps & (byte)enumInterfaces.JTAG) != 0)
    {
        d.isJTAG = true;
        Protocol_list.Add("JTAG");
    }
}


How do I link the two? How do I make sure the d device gets loaded the property values from Protocol_list and have it displayed as a drop down in the property grid?

Thank you,
H-
Posted

1 solution

Hi,

This should help you on the way.

1. Make your list a member of the class that has an instance of the property class.
C#
internal List<string> Protocol_list</string>


2. Fill the list with your method GetProgrammerProtocols

3. Put a break point inside GetStandardValues
Use the watch and inspect the parameter
C#
ITypeDescriptorContext context

You should find your calling class there and can use your list.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900