C# 列表框显示“;名称空间.Accounts“;而不是实际文本

C# 列表框显示“;名称空间.Accounts“;而不是实际文本,c#,wpf,C#,Wpf,可能重复: 我正在尝试实现一个解决方案,在通过文本框和添加按钮添加项目后,将在列表框中显示项目。。。作为WPF的新手,我几乎已经明白了这一点(在一周的时间里,我的头撞在了我的桌子上……哈哈) 问题是列表框中填充的是“WinBudget.Accounts”,而不是在列表框中键入的实际项目 请帮忙 型号 // the model is the basic design of an object containing properties // and methods of that object.

可能重复:

我正在尝试实现一个解决方案,在通过文本框和添加按钮添加项目后,将在列表框中显示项目。。。作为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));
      }
    }
}
// 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});
    }
}
列表框XAML

 <ListBox Name="MyAccounts" />

代码隐藏

// 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));
      }
    }
}
// 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});
    }
}
//创建帐户集合,然后每当单击按钮时,
//创建新帐户对象并添加到集合中。
公共部分类Window1:Window
{
私有ObservableCollection AccountList=新ObservableCollection();
公共窗口1()
{
初始化组件();
AccountList.Add(新帐户{AccountName=“我的帐户”});
this.MyAccounts.ItemsSource=AccountList;
}
私有无效okBtn_单击(对象发送方,路由目标e)
{
AccountList.Add(新帐户{AccountName=accountaddTextBox.Text});
}
}

默认情况下,
列表框的项模板显示的是调用对象的
ToString()
的结果。因此,您必须重写Account.ToString()
,或者提供不同的模板。

WPF的UI可以包含任何类型的对象,甚至包括类,例如
Account
。如果WPF在它不理解的UI中遇到一个对象,它将使用包含该对象的
.ToString()
文本块来呈现它

在您的例子中,
ListBox.Item
是一个
Account
对象,因此它是通过返回类名的
Account.ToString()
呈现的

有许多不同的处理方法。我的首选方法通常是使用
DataTemplate
。这将创建一个应用程序范围的模板,告诉WPF如何绘制特定类型的对象

<Window.Resources>
    <DataTemplate TargetType="{x:Type local:Account}">
        <TextBlock Text="{Binding AccountName}" />
    </DataTemplate>
</Window.Resources>
如果您的模板很简单,如图所示,您还可以设置列表框的
DisplayMemberPath
,使默认模板与上图所示的模板类似,绑定指向您放入的任何内容
DisplayMemberPath

<ListBox Name="MyAccounts" DisplayMemberPath="AccountName" />


当然,您也可以覆盖
Account
对象的
ToString()
方法,以返回
AccountName
,而不是默认的类名,但是我不建议在这种情况下使用它。

最好绑定到
Account::AccountName
属性,但若你们不知道或者因为任何原因不能,那个么快速而肮脏的解决方案可能是可行的

公共类帐户:INotifyPropertyChanged { 私有字符串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));
  }
}




   //NEW
   public override string ToString()
   {
       return AccountName;
   }

}

@lance提到了那个线程。DisplayMemberPath=“AccountName”起作用了…非常感谢!!我更喜欢使用ToString()来显示诊断信息,而不是UI需要。