我为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,另一个用于textbox。我尝试过以下方法:
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”行运行。有任何建议,请
答案 0 :(得分: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);