<p>I know in ASP.NET I can get an item from a DropDownList by using</p>

<pre><code>DropDownList1.Items.FindByText
</code></pre>

<p>Is there a similar method I can use in WPF for a ComboBox?</p>

<p>Here's the scenario.</p>

<p>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.</p>

<p>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.</p>

<hr>

<p>Here's the scenario.</p>

<p>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.</p>

<p>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.</p>

<p>Does this make sense?</p>

[{'answer_id': 41305, 'author': 'aku', 'author_id': 1196, 'author_profile': 'https://Stackoverflow.com/users/1196', 'pm_score': 2, 'selected': False, 'text': '<p>In WPF you can use FindName method.</p>\n\n<p>XAML:</p>\n\n<pre><code>    &lt;ComboBox Name="combo"&gt;\n        &lt;ComboBoxItem Name="item1" &gt;1&lt;/ComboBoxItem&gt;\n        &lt;ComboBoxItem Name="item2"&gt;2&lt;/ComboBoxItem&gt;\n        &lt;ComboBoxItem Name="item3"&gt;3&lt;/ComboBoxItem&gt;\n    &lt;/ComboBox&gt;\n</code></pre>\n\n<p>Code-behind file</p>\n\n<pre><code>   item1.Content = "New content"; // Reference combo box item by name\n   ComboBoxItem item = (ComboBoxItem)this.combo.FindName("item1"); // Using FindName method\n</code></pre>\n\n<p>To find item by its content you can use <a href="http://msdn.microsoft.com/en-us/library/ms747327.aspx" rel="nofollow noreferrer">UI automation</a>.</p>\n'}, {'answer_id': 99549, 'author': 'SmartyP', 'author_id': 18005, 'author_profile': 'https://Stackoverflow.com/users/18005', 'pm_score': 1, 'selected': False, 'text': "<p>instead of trying to bind the SelectedIndex why don't you just bind the SelectedItem in the ComboBox to the value in the record?</p>\n\n<p>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'.. </p>\n\n<p>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.</p>\n"}, {'answer_id': 662946, 'author': 'Rich', 'author_id': 53501, 'author_profile': 'https://Stackoverflow.com/users/53501', 'pm_score': 5, 'selected': True, 'text': '<p>Can you use ItemContainerGenerator?</p>\n\n<p>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:</p>\n\n<pre><code>ComboBoxItem item = \n    (ComboBoxItem)myComboBox.ItemContainerGenerator.ContainerFromItem(myObject);\n</code></pre>\n'}, {'answer_id': 662950, 'author': 'Arcturus', 'author_id': 900, 'author_profile': 'https://Stackoverflow.com/users/900', 'pm_score': 0, 'selected': False, 'text': '<p>You can retrieve combobox items in two ways:</p>\n\n<p>By item:</p>\n\n<pre><code>ComboBoxItem item = (ComboBoxItem) control.ItemContainerGenerator.ContainerFromItem(control.SelectedItem);\n</code></pre>\n\n<p>By index:</p>\n\n<pre><code>ComboBoxItem item = (ComboBoxItem) control.ItemContainerGenerator.ContainerFromIndex(1);\n</code></pre>\n'}, {'answer_id': 662969, 'author': 'Denis Troller', 'author_id': 29336, 'author_profile': 'https://Stackoverflow.com/users/29336', 'pm_score': 0, 'selected': False, 'text': '<p>Can you give some context as to what exactly you are trying to do?</p>\n\n<p>What objects do you put in your Combobox, and using which method? (Are you setting or binding the ItemsSource property?)\nWhy 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?</p>\n\n<p>Worst case, you can perform the search on the collection to which you bind your ComboBox using Linq To Objects.</p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/41304', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/71/']