Note that there are some explanatory texts on larger screens.

plurals
  1. POListBox showing "Namespace.Accounts" instead of actual text
    text
    copied!<blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/1659157/wpf-listbox-display-properties-of-the-itemsource">WPF ListBox - Display properties of the ItemSource</a> </p> </blockquote> <p>I'm trying to implement a solution that will show items in a ListBox after adding them via a text box and Add button... Being new to WPF, I've almost got this figured out (after a week of banging my head against my desk...lol)</p> <p>The problem is that the listbox is being populated with the "WinBudget.Accounts" instead of the actual item that is typed into the list box.</p> <p>Please help.</p> <p><strong>MODEL</strong> </p> <pre><code>// the model is the basic design of an object containing properties // and methods of that object. This is an account object. public class Account : 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></pre> <p><strong>ListBox XAML</strong></p> <pre><code> &lt;ListBox Name="MyAccounts" /&gt; </code></pre> <p><strong>CODE BEHIND</strong> </p> <pre><code>// create a collection of accounts, then whenever the button is clicked, //create a new account object and add to the collection. public partial class Window1 : Window { private ObservableCollection&lt;Account&gt; AccountList = new ObservableCollection&lt;Account&gt;(); public Window1() { InitializeComponent(); AccountList.Add(new Account{ AccountName = "My Account" }); this.MyAccounts.ItemsSource = AccountList; } private void okBtn_Click(object sender, RoutedEventArgs e) { AccountList.Add(new Account{ AccountName = accountaddTextBox.Text}); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload