English 中文(简体)
ListBox 显示“Namespace”。 账户”而不是实际案文[复制]
原标题:ListBox showing "Namespace.Accounts" instead of actual text [duplicate]
  • 时间:2012-01-13 15:18:29
  •  标签:
  • c#
  • wpf
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
WPF ListBox - Display properties of the ItemSource

I m 试图通过一个文本箱和Addutton...在清单Box中展示物品的解决办法 我是妇联的新鲜事,我几乎不谈这个数字(在一周禁止我头对我的桌子......鲁尔之后)

问题在于,名单箱正在配备“WinBudget”。 账户”而不是列入清单箱的实际项目。

请提供帮助。

// 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));
      }
    }
}

 <ListBox Name="MyAccounts" />

http://www.ohchr.org。

// 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});
    }
}
问题回答

WPF UI 能够包含任何类型的物体,甚至包括Account。 如果世界森林论坛在《国际不动产合同》中遇到一个标的,那么它将使用包含<代码>的

在您的情形下,ListBox.ItemAccount Object, 即以Account.ToString(<>为重,该代码为平级名称。

处理该问题的方法多种多样。 我倾向于使用<代码>DataTemplate。 由此形成了一个全方位的模板,该模板将告诉世界森林论坛如何选择特定类型的物体。

<Window.Resources>
    <DataTemplate TargetType="{x:Type local:Account}">
        <TextBlock Text="{Binding AccountName}" />
    </DataTemplate>
</Window.Resources>

另一种方法是超标<代码>ListBox.ItemTemplate,以具体说明<代码>ItemTemplate 供您使用ListBox。 我通常会使用这一模板,以用于<代码>ListBox。 仅限

<ListBox Name="MyAccounts">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding AccountName}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果你的模板简单,如所示,你还可以制定名单Box s代码<>Display MembersPath,使缺省模板像上面所示,并注明了你在中的任何内容。 显示会员资格/代码

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

当然,你也可以超出以下标准:<代码>Account 反对s sToString(<>/code> 退回AccountName,而不是违约类别Name,但我在此情况下不建议这样做。

<代码>ListBox在项目模板中注明了标语的<代码>ToString(。 因此,你要么必须优先于<代码>Account.ToString(,要么提供不同的模板。

最好对<代码>具有约束力。 账户:AccountName 财产,但如果您不提供或因任何原因不能提供,那么快速和 d脏的解决办法可能是

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));
  }
}




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

}




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签