Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find item in WPF ComboBox

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?

like image 804
Dillie-O Avatar asked Sep 03 '08 08:09

Dillie-O


People also ask

How do I find the value of a ComboBox?

Suppose your ComboBox name is comboBoxA . Then its value can be gotten as: string combo = comboBoxA. SelectedValue.

What happens when we select a value from a ComboBox in WPF?

A ComboBox control is an items control that works as a ListBox control but only one item from the collection is visible at a time and clicking on the ComboBox makes the collection visible and allows users to pick an item from the collection. Unlike a ListBox control, a ComboBox does not have multiple item selection.

What is ComboBox ItemTemplate?

Data binding the ComboBox Each item, as defined by the ItemTemplate, consists of a StackPanel with a Rectangle and a TextBlock, each bound to the color value.


1 Answers

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);
like image 166
Rich Avatar answered Oct 08 '22 02:10

Rich