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?
Suppose your ComboBox name is comboBoxA . Then its value can be gotten as: string combo = comboBoxA. SelectedValue.
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With