William Thomas Waller
William Thomas Waller

Reputation: 697

Naming Items in a Listbox

I have a ListBox populated by a List of a certain class. However when each element is added to the ListBox its name in the ListBox is its class hierarchy, but I want to be able to rename the items since it is supposed to be a Layer list for rendering teach Listbox item on a screen.

What attribute of the item does ListBox look for when choosing a name for each element? And how can I allow the user to rename items?

Upvotes: 1

Views: 174

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61349

Be default, a ListBox will just call ToString on every object in its ItemsSource. For a class, this is just the namespace-qualified name unless ToString is overriden.

There are three ways to change what is displayed:

  1. Override ToString()
  2. Use the DisplayMemberPath property to bind to a property
  3. Use a custom ItemTemplate

You could use a TextBox in your ItemTemplate to allow for rename, or another control (elsewhere on the page) that updates the backing property (therefore updating the bound object).

Upvotes: 1

Related Questions