tags:

views:

1001

answers:

2

Hi!

Is there a way to disable all but the first x items of a WPF ComboBox by accessing its items' properties via an Index?

edit:

 ComboBoxItem DiffieHellmanItem = (ComboBoxItem)comboboxName.ItemContainerGenerator.ContainerFromIndex(i));

works

A: 

I don't know of a way actually to disable items in a ComboBox, but you should be able to add logic to your OnClick event which will check if the selected item's index is >= x and disallow whatever operation was requested based on that.

AllenG
+1  A: 

EDIT: Looks like you found a way to get at the ComboBoxItems. You might also want to look at ItemContainerStyle and see if there's some DataBinding you can take advantage of (if your items have an Enabled property or something similar).

However, depending on how you are doing your DataBinding, you may be able to display your "disabled" items in a different Brush, Opacity, etc. by using DataTemplates. Bea Stollnitz has a good post about that here.

One thing you might want to consider from a UX standpoint is why you want to make them disabled as opposed to hidden. Is there some sort of progressive disclosure tactic that you can take advantage of to not have a ComboBox filled with things the user can't click?

micahtan
well, I could hide them as well. The problem is rather that I did not find a way to iterate through the ComboBoxItems or access them via an index.
Learn something new every day. Got to be a way to take advantage of DataBinding and the container generation to do this outside of the code-behind?
micahtan