views:

7459

answers:

11

I know in ASP.NET I can get an item from a DropDownList by using

DropDownList1.Items.FindByText

Is there a similar method I can use in WPF for a ComboBox?

A: 

In WPF you can use FindName method.

XAML:

    <ComboBox Name="combo">
        <ComboBoxItem Name="item1" >1</ComboBoxItem>
        <ComboBoxItem Name="item2">2</ComboBoxItem>
        <ComboBoxItem Name="item3">3</ComboBoxItem>
    </ComboBox>

Code-behind file

   item1.Content = "New content"; // Reference combo box item by name
   ComboBoxItem item = (ComboBoxItem)this.combo.FindName("item1"); // Using FindName method

To find item by its content you can use UI automation.

aku
A: 

In winforms you can find an index based on a string:

ComboBox box=new ComboBox();
int index=box.FindString("SomeString");

There is also a method FindStringExact

Kjetil Watnedal
ok, but the expected answers are all about WPF, not WinForms
Junior Mayhé
@Junior Mayhé: The fact that the question was about WPF was not stated in the original question: http://stackoverflow.com/posts/41304/revisions
Kjetil Watnedal
A: 

In my particular instance, I'm doing dynamic databinding to the control in question. I am using the "Name" field from my result set as the display binding.

Is that going to change things at all?

Dillie-O
This is not a forum. You should edit your original question, not ammend it by posting a non-answer as an answer.
Josh G
<smiles /> True true Josh. However this was done far before etiquette was actually established for this. Guess I'll get off my lazy duff one of these days. 8^D
Dillie-O
+1  A: 

Why are you searching the ComboBox instead of searching the actual source of the data?

Sekkusu
A: 

Here's the scenario.

I have a table called RestrictionFormat that contains a column called RestrictionType, the type is a foreign key to a table that stores these values.

In my editor application I'm writing, when the user selects the RestrictionFormat from a ComboBox (this works fine), I'm pulling up the details for editing. I'm using a second ComboBox to make sure the user only selects one RestrictionType when editing. I already have the second combobox bound property from the RestrictionType table, but I need to change the selected index on it to match the value specified in the record.

Does this make sense?

Dillie-O
A: 

Quick update:

ComboBox.FindString("Traditional")

comes back with an error that the FindString method is not a member. I'm wondering if this is a WinForms specific method as opposed to WPF (I'll update my title accordingly).

ComboBox.FindName("Traditional")

comes back with Nothing as the result. I wager that the method is looking for the Id or variable name of a ComboBox control, and I'm looking for a ComboBox item with the name or value of ("Traditional"). I figure I can fine tune it after I find something that will search the list.

Any other ideas?

Dillie-O
Again, the question it's not about WinForms
Junior Mayhé
Notice that System.Windows.Controls.ComboBox.FindName() method doesn't exist for WPF. I think you could be messing around with namespaces
Junior Mayhé
+1  A: 

instead of trying to bind the SelectedIndex why don't you just bind the SelectedItem in the ComboBox to the value in the record?

in other words, set the DataContext of the ComboBox (or its parent) to the selected 'record' and bind the SelectedItem on the ComboBox to an exposed property on the 'record'..

it may help if you could provide some code snippets, or extra details so that responses can be more specific and refer to the variables and types you are using in both the source record and the ComboBox which you have populated.

SmartyP
+3  A: 

Can you use ItemContainerGenerator?

ItemContainerGenerator contains a ContainerFromItem method that takes an object parameter. If you have a reference to the full object that your comboBox contains (or a way to reconstruct it), you can use the following:

ComboBoxItem item = 
    (ComboBoxItem)myComboBox.ItemContainerGenerator.ContainerFromItem(myObject);
Rich
Hehe you beat me to it :)
Arcturus
A: 

You can retrieve combobox items in two ways:

By item:

ComboBoxItem item = (ComboBoxItem) control.ItemContainerGenerator.ContainerFromItem(control.SelectedItem);

By index:

ComboBoxItem item = (ComboBoxItem) control.ItemContainerGenerator.ContainerFromIndex(1);
Arcturus
A: 

Can you give some context as to what exactly you are trying to do?

What objects do you put in your Combobox, and using which method? (Are you setting or binding the ItemsSource property?) Why do you need to lookup an item by its "text"? The most usual usage in WPF is to bind the SelectedItem property to something else so you can retrieve/set the selected entry using your representation. Is there a specific requirement for which you need to find a specific item in the list?

Worst case, you can perform the search on the collection to which you bind your ComboBox using Linq To Objects.

Do not mistake the ComboBoxItem (that is, the element generated for you behind the scenes by WPF when you bind ItemsSource) with the SelectedItem, which is the actual object in the collection you bind to. That usually is the source of most problems whith WPF when you are not used to it. There are precious few cases when you need to find the actual ComboBoxItem.

Denis Troller
A: 

ComboBox is just a View for your data... I don't think it's a good idea doing the UI-driven development in such a case. You should perform a search against the data source or items collection rather than UI control itself. Otherwise you might stuck with being bound to a specific implementation of UI part that is difficult to maintain and support in future.

Denis Vuyka