2

我为 XML 反序列化创建了以下类:

[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class ProgrammingConfiguration
{
    private uint versionField;
    private byte[] eLFImageField;
    private PreferenceType[] preferencesField;
    private ProgrammingConfigurationProtectFlashAfterProgramming   protectFlashAfterProgrammingField;

    //XML version
    public uint Version
    {
        get { return this.versionField; }
        set { this.versionField = value; }
    }

    //ELF image
    [System.Xml.Serialization.XmlElementAttribute(DataType = "base64Binary")]
    public byte[] ELFImage
    {
        get { return this.eLFImageField; }
        set { this.eLFImageField = value; }
    }

    //Preference
    [System.Xml.Serialization.XmlArrayItemAttribute("Preference", IsNullable = false)]
    public PreferenceType[] Preferences
    {
        get { return this.preferencesField; }
        set { this.preferencesField = value; }
    }

    //Protect flash after programming
    public ProgrammingConfigurationProtectFlashAfterProgramming ProtectFlashAfterProgramming
    {
        get { return this.protectFlashAfterProgrammingField; }
        set { this.protectFlashAfterProgrammingField = value; }
    }
}

//Preference Type
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class PreferenceType
{
    private string tagField;
    private PreferenceTypePrecedence precedenceField;
    private ValueType valueField;

    public string Tag
    {
        get { return this.tagField; }
        set { this.tagField = value; }
    }

    public PreferenceTypePrecedence Precedence
    {
        get { return this.precedenceField; }
        set { this.precedenceField = value; }
    }

    public ValueType Value
    {
        get { return this.valueField; }
        set { this.valueField = value; }
    }
}

//Precedence Type
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum PreferenceTypePrecedence
{
    XML,
    [System.Xml.Serialization.XmlEnumAttribute("Target,XML")]
    TargetXML,
    Target,
}

//Value Type
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class ValueType
{
    private string lengthField;
    private object itemField;
    private ItemChoiceType itemElementNameField;

    [System.Xml.Serialization.XmlElementAttribute(DataType = "integer")]
    public string Length
    {
        get { return this.lengthField; }
        set { this.lengthField = value; }
    }

    [System.Xml.Serialization.XmlElementAttribute("AutoIncrement", typeof(ValueTypeAutoIncrement))]
    [System.Xml.Serialization.XmlElementAttribute("HexBinary", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("Integer", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("String", typeof(string))]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
    public object Item
    {
        get { return this.itemField; }
        set { this.itemField = value; }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemChoiceType ItemElementName
    {
        get { return this.itemElementNameField; }
        set { this.itemElementNameField = value; }
    }
}

//Autoincrement data
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ValueTypeAutoIncrement
{
    private string minimumField;
    private string maximumField;
    private ValueTypeAutoIncrementOverflowBehaviour overflowBehaviourField;

    public string Minimum
    {
        get { return this.minimumField; }
        set { this.minimumField = value; }
    }

    public string Maximum
    {
        get { return this.maximumField; }
        set { this.maximumField = value; }
    }

    public ValueTypeAutoIncrementOverflowBehaviour OverflowBehaviour
    {
        get { return this.overflowBehaviourField; }
        set { this.overflowBehaviourField = value; }
    }
}

//Overflow behaviour
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum ValueTypeAutoIncrementOverflowBehaviour
{
    Wrap,
    Exception,
}

//Data type
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(IncludeInSchema = false)]
public enum ItemChoiceType
{
    AutoIncrement,
    HexBinary,
    Integer,
    String,
}

//ProtectFlashAfterProgramming
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum ProgrammingConfigurationProtectFlashAfterProgramming
{
    True,
    False,
}

我想创建 2 个绑定源,一个用于 datagridview,一个用于文本框。我尝试了以下方法:

        ProgrammingConfiguration pc = new ProgrammingConfiguration();

        XmlSerializer serializer = new XmlSerializer(typeof(ProgrammingConfiguration));

        //Reading the XML document requires a FileStream.            
        Stream reader = new FileStream(filename, FileMode.Open);

        //Call the Deserialize method to restore the object's state.
        pc = (ProgrammingConfiguration)serializer.Deserialize(reader);

        reader.Close();

        bindingsource.DataSource = pc;
        bindingsource.DataMember = "Preferences";


        bsV.DataSource = bindingsource;
        bsV.DataMember = "ItemElementName";

该程序无法在 bsV.DataMember = "ItemElementName" 行运行。请有任何建议

4

1 回答 1

0

改变:

bindingsource.DataSource = pc;

到:

bindingsource.DataSource = new List { pc };

我不知道为什么,但 PropertyManager 不喜欢链接属性,但 CurrencyManager 喜欢。使用 IEnumerable 将欺骗并强制 BindingContext 创建 CurrencyManager 而不是用于绑定单个对象的 PropertyManager。该问题与 BindingSource 无关,您可以将网格的 DataSource 属性设置为列表,将 DataMember 设置为 Preferences。

您也不必使用 BindingSource 绑定到文本框。

只需使用:

textBox.DataBindings.Add(list, "Preferences.ItemElementName");

至于 Value.Maximum 的使用:

textBoxMaximum.DataBindings.Add(list, Preferences.Value.Maximum);
textBoxMaximum.DataBindings.Add(bindingSource, Value.Maximum);
于 2012-10-04T03:15:29.257 回答