以编程方式定义属性内容

时间:2014-02-08 17:38:41

标签: c#

我正在编写一个程序,它将连接到I2C转USB控制器。我正在使用WPF。  我决定创建一个Device类来包含硬件描述及其所有属性,这样我就可以使用它们来显示属性并让最终用户修改硬件的设置。  我有上面的大部分工作,除了2个属性。  大多数属性是字符串或int或byte等...所以它们很容易使用,如下例所示:

private string _name;
[Category("General")]
[ReadOnly(false)]
[DisplayName("Module Name")]
[Description("Enter descriptive string ")]
public string Name { get { return _name; } set { _name = value; OnPropertyChanged("Module Name"); } }

我很难掌握2个属性,需要填写一个列表 所以我读了硬件设置,它返回它支持I2C,SPI和UART 现在我希望能够将它们传递给“CommunicationType”属性,并将其显示为属性窗口中的列表。
要查看我的程序是否正常工作,我创建了以下类:

public class CommunicationType : IItemsSource
{
    public ItemCollection GetValues()
    {
        ItemCollection sizes = new ItemCollection();
        sizes.Add((int)PP_COM_Wrapper.enumInterfaces.I2C, "I2C");
        sizes.Add((int)PP_COM_Wrapper.enumInterfaces.ISSP, "ISSP");
        sizes.Add((int)PP_COM_Wrapper.enumInterfaces.JTAG, "JTAG");
        sizes.Add((int)PP_COM_Wrapper.enumInterfaces.SPI, "SPI");
        sizes.Add((int)PP_COM_Wrapper.enumInterfaces.SWD, "SWD");
        sizes.Add((int)PP_COM_Wrapper.enumInterfaces.SWD_SWV, "SWD SWV");
        return sizes;
    }
}

然后在属性中,我有以下内容:

private int _comtype;
[Category("Settings")]
[ReadOnly(false)]
[ItemsSource(typeof(CommunicationType))]
[DisplayName("Communication Type")]
public int CommunicationType { get { return _comtype; } set { _comtype = value; OnPropertyChanged("Communication Type"); } }

因此,当我运行它时,它使用户可以从下拉菜单中选择一种通信类型 现在问题是,我如何以编程方式填充它?有些硬件只支持I2C和SPI,所以我只想根据硬件用2个项目填充列表。

0 个答案:

没有答案
相关问题