WPF equivalent of DropDownList?

  • Thread starter Thread starter Jeremy
  • Start date Start date
J

Jeremy

I'm getting started with WPF & am converting a small asp.net app that uses a
couple of dropdownlists. They have the useful feature of holding a string
and also another (index) value in each item. ComboBox does not seem to have
this virtue. Suggestions?
 
dropdownlist should work for you just fine. what issues are you having?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
 
Alvin, so where on the toolbox is the ddl? Common goes from ComboBox to
Grid (no "D"). Controls goes from DocumentViewer to Ellipse. General is
empty.


Alvin Bruney said:
dropdownlist should work for you just fine. what issues are you having?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The O.W.C. Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $19.99
-------------------------------------------------------


Jeremy said:
I'm getting started with WPF & am converting a small asp.net app that
uses a couple of dropdownlists. They have the useful feature of holding a
string and also another (index) value in each item. ComboBox does not
seem to have this virtue. Suggestions?
 
Jeremy said:
I'm getting started with WPF & am converting a small asp.net app that uses
a couple of dropdownlists. They have the useful feature of holding a
string and also another (index) value in each item. ComboBox does not
seem to have this virtue. Suggestions?

In the WPF ComboBox the items can be any type of object, so one solution is
to define a class with two properties - the string and the index value - and
add instances of this class to your ComboBox (or bind a collection of
instances to its ItemsSource property).

Another option is to just add a string to the ComboBox and then use the
ComboBox.ItemContainerGenerator.ContainerFromItem() method (or
ContainerFromIndex) to give you the ComboBoxItem which WPF automatically
creates to actually hold the string you've just added. The ComboBoxItem has
a Tag property which you can use to hold your other value. However, I think
the first way is better.

Dr. WPF has written a very good set of blog posts on WPF ItemsControls (of
which ComboBox is one) - see
http://drwpf.com/blog/Home/tabid/36/EntryID/32/Default.aspx for example.

Chris Jobson
 
Chris, thanks for the pointer. Dr. WPF is a great site.

I've got this working, except for re-populating the itemsSource.

I have a list of objects which I use to populate the source. User picks one
and I populate some textboxes. When the user makes a change and saves it to
a database, I create a new List<T>, load it from the database and assign it
to itemsSource. I do the reload after save because I don't want to have to
detect whether the user added a new item or merely edited an existing one.

The problem is, after saving, the dropdown now has only 2 items showing, but
the actual itemsSource list has 4 items.

I figure there is some kind of reset that needs to be done, but haven't
found a method yet.

Any ideas?

Jeremy
 
Jeremy said:
Chris, thanks for the pointer. Dr. WPF is a great site.

I've got this working, except for re-populating the itemsSource.

I have a list of objects which I use to populate the source. User picks
one and I populate some textboxes. When the user makes a change and saves
it to a database, I create a new List<T>, load it from the database and
assign it to itemsSource. I do the reload after save because I don't want
to have to detect whether the user added a new item or merely edited an
existing one.

The problem is, after saving, the dropdown now has only 2 items showing,
but the actual itemsSource list has 4 items.

I figure there is some kind of reset that needs to be done, but haven't
found a method yet.

Any ideas?

That sounds very strange. Normally changing the ItemsSource is all that's
needed. A couple of ideas:
- I assume you first populated the list using the ItemsSource rather than
setting the Items property. If not I'm not sure what happens if both Items
and ItemsSource are set, but I'd suggest setting Items to null first.
- Is it definitely a NEW List<T> that you are assigning to ItemsSource
(rather than re-using the same List<T> but clearing and reloading the items
in the list)? If you are re-using the same List<T> object then setting
ItemsSource to null and then back to the List should work.

If you haven't done so already, it might be worth looking at
ObservableCollection<T> instead of List<T>, since that will automatically
notify any controls bound to it of changes to the items in the collection.

Good luck!
Chris
 
Jeremy said:
Chris, thanks for the pointer. Dr. WPF is a great site.

I've got this working, except for re-populating the itemsSource.

I have a list of objects which I use to populate the source. User picks
one and I populate some textboxes. When the user makes a change and saves
it to a database, I create a new List<T>, load it from the database and
assign it to itemsSource. I do the reload after save because I don't want
to have to detect whether the user added a new item or merely edited an
existing one.

The problem is, after saving, the dropdown now has only 2 items showing,
but the actual itemsSource list has 4 items.

I figure there is some kind of reset that needs to be done, but haven't
found a method yet.

Any ideas?

That sounds very strange. Normally changing the ItemsSource is all that's
needed. A couple of ideas:
- I assume you first populated the list using the ItemsSource rather than
setting the Items property. If not I'm not sure what happens if both Items
and ItemsSource are set, but I'd suggest setting Items to null first.
- Is it definitely a NEW List<T> that you are assigning to ItemsSource
(rather than re-using the same List<T> but clearing and reloading the items
in the list)? If you are re-using the same List<T> object then setting
ItemsSource to null and then back to the List should work.

If you haven't done so already, it might be worth looking at
ObservableCollection<T> instead of List<T>, since that will automatically
notify any controls bound to it of changes to the items in the collection.

Good luck!
Chris
 
Jeremy said:
Chris, thanks for the pointer. Dr. WPF is a great site.

I've got this working, except for re-populating the itemsSource.

I have a list of objects which I use to populate the source. User picks
one and I populate some textboxes. When the user makes a change and saves
it to a database, I create a new List<T>, load it from the database and
assign it to itemsSource. I do the reload after save because I don't want
to have to detect whether the user added a new item or merely edited an
existing one.

The problem is, after saving, the dropdown now has only 2 items showing,
but the actual itemsSource list has 4 items.

I figure there is some kind of reset that needs to be done, but haven't
found a method yet.

Any ideas?

That sounds very strange. Normally changing the ItemsSource is all that's
needed. A couple of ideas:
- I assume you first populated the list using the ItemsSource rather than
setting the Items property. If not I'm not sure what happens if both Items
and ItemsSource are set, but I'd suggest setting Items to null first.
- Is it definitely a NEW List<T> that you are assigning to ItemsSource
(rather than re-using the same List<T> but clearing and reloading the items
in the list)? If you are re-using the same List<T> object then setting
ItemsSource to null and then back to the List should work.

If you haven't done so already, it might be worth looking at
ObservableCollection<T> instead of List<T>, since that will automatically
notify any controls bound to it of changes to the items in the collection.

Good luck!
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top