可能重复:
WPF ListBox - Display properties of the ItemSource列表框显示“Namespace.Accounts”,而不是实际的文本
我想实现一个解决方案添加它们后,将显示在ListBox项目通过一个文本框和添加按钮...作为WPF的新手,我几乎得到了这个想法(经过了一个星期的头撞我的桌子...大声笑)
问题是,列表框是正在填充“WinBudget.Accounts”而不是输入到列表框中的实际项目。
请帮忙。
模型
// 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));
}
}
}
列表框XAML
<ListBox Name="MyAccounts" />
后面的代码
// 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<Account> AccountList = new ObservableCollection<Account>();
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});
}
}
@lance,提到那个线程。 DisplayMemberPath =“AccountName”工作......非常感谢! – Woody 2012-01-13 15:24:36