April 7th, 2009, 02:35 PM
How to get the text value of a bound data item in code??
I have a ComboBox that has it's ItemsSource databound - I'm using the DisplayMemberPath to set the actual text that displays in the control.
I have a function where I need to iterate through the Items collection of the ComboBox and check the text value for each of the items ... the problem is, of course, that the ToString() for each of the combo box items is actually just a class name. Is there a good way to extract the text value for the ComboBoxItems by using the DisplayMemberPath somehow in code?
April 9th, 2009, 03:07 PM
Re: How to get the text value of a bound data item in code??
When iterating the Items collection, cast each item to the data type that is bound to your ComboBox and access the desired property. For example:
foreach (var item in this.comboBox.Items)
string textProperty = ((MyObject)item).MyTextProperty;
Here MyObject is the name of the data object I bound to ItemsSource and MyTextProperty is what I used for DisplayMemberPath.
Click Here to Expand Forum to Full Width
This is a Codeguru.com survey!