Here's my situation. I have a solution coded where I type a string into a textbox and upon clicking an Add button, it populates the Listbox.
Now, I want to:
a) Save that string to an XML file immediately. b) When the window opens, I want to display the data from that XML file in the listbox
Here's what I got so far:
Class
public class Accounts : INotifyPropertyChanged
{
private string m_AccountName;
public event PropertyChangedEventHandler PropertyChanged;
public string AccountName
{
get { return m_AccountName; }
set
{
m_AccountName = value;
OnPropertyChanged("AccountName");
}
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
}
Code Behind
public partial class Account : Window
{
private ObservableCollection<Accounts> AccountList = new ObservableCollection<Accounts>();
public Account()
{
InitializeComponent();
this.accountListBox.ItemsSource = AccountList;
}
private void addBtn_Click(object sender, RoutedEventArgs e)
{
AccountList.Add(new Accounts { AccountName = accountaddTextBox.Text });
}
XAML
<ListBox DisplayMemberPath="AccountName" Height="164" HorizontalAlignment="Left" Margin="12" Name="accountListBox" VerticalAlignment="Top" Width="161" />
This code works for populating the listbox after you hit the Add button.
I've tried adding an instance of XMLTextReader to Window_Loaded and use an ArrayList as well to try to read the XML file and load it, but when I use ItemsSource it comes back with an error that I have to use ItemControl.ItemsSource...
Here's what I have tried, but it fails:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
XmlTextReader reader = new XmlTextReader("Accounts.xml");
ArrayList ar = new ArrayList();
// Loop over the XML file
while (reader.Read())
{
// Here we check the type of the node, in this case we are looking for element
if (reader.NodeType == XmlNodeType.Element)
{
// If the element is "accounts"
if (reader.Name == "Accounts")
{
ar.Add(reader.Value);
accountListBox.ItemsSource = ar;
}
}
}
reader.Close();
}