Question

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?

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.


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?

Was it helpful?

Solution

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

OTHER TIPS

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.

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top