Is it possible to databind to an object in memory and also allow for databinding to XML
I have created an object called Project that has different properties (strings and some custom objects), I have bound text fields to these properties to get user input. I have created a method that outputs this object to an XML file. However when I import this XML file back into memory the text fields do not become populated with the text or list views of some custom objects that inher开发者_JAVA技巧it from ObservableCollection do not have any text. The XML does load properly since if I enter text into the empty fields it updates the property and I can export an XML file with the new values.
To load the xml I use the following code
public void LoadXML()
{
OpenFileDialog fileDialog = new OpenFileDialog();
fileDialog.Title = "Load XML File";
fileDialog.Filter = "XML Files|*.xml";
DialogResult result = fileDialog.ShowDialog();
if (result.ToString().Equals("OK"))
{
string filePath = fileDialog.FileName.ToString();
XmlSerializer serializer = new XmlSerializer(typeof(Project));
TextReader textReader = new StreamReader(filePath);
newProject = (Project)serializer.Deserialize(textReader);
textReader.Close();
}
}
Any suggestions would be welcomed, thanks.
I assume you use WPF.
You need to implement the INotfiyPropertyChanged-Interface and throw its event for every Property of your class that is tied to a Control.
WPF then updates your GUI accordingly, when you deserialize the Project from XML. If it does not, check if the DataContext of your control is set to the Project instance that you deserialized.
精彩评论