如何动态填充属性的值 [英] How Do I Dynamically Populate Values For A Property

查看:49
本文介绍了如何动态填充属性的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在开发一个项目,我需要动态地将值添加到ProgrammerDevice类型的对象的属性中。

我正在使用C#,WPF 。



我现在的代码是我得到的代码:

[ ^ ]



在我的对象类中,我有以下属性。

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.

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:

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:

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");
    }
}





如何链接两者?如何确保d设备从Protocol_list加载属性值并将其显示为属性网格中的下拉菜单?



谢谢,

H-



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-

推荐答案

您好,



这应该可以帮助您。



1.使您的列表成为具有属性类实例的类的成员。

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.
internal List<string> Protocol_list</string>





2.使用您的方法填写列表GetProgrammerProtocols



3.在内设一个断点?GetStandardValues

使用手表并检查参数



2. Fill the list with your method GetProgrammerProtocols

3. Put a break point inside GetStandardValues
Use the watch and inspect the parameter

ITypeDescriptorContext context



您应该在那里找到您的呼叫类,并且可以使用您的列表。


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


这篇关于如何动态填充属性的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆