How to get the text value of a bound data item in code??
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to get the text value of a bound data item in code??

  1. #1
    Join Date
    Apr 2009
    Posts
    1

    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?

  2. #2
    Join Date
    Jan 2009
    Posts
    36

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center