none
How to get SelectedText from a combobox?

    Question

  • I've got a WPF Win8 Store form with many controls, a.o. comboboxes. E.g.:

    <ComboBox x:Name="cb1"  SelectedValuePath="Nr" SelectedValue="{Binding cb1,Mode=TwoWay}" Margin="244,430,174,0" VerticalAlignment="Top" Height="30" ItemsSource="{Binding OCWithData}"  DisplayMemberPath="Descr" />


    With the following code I can get the key (Nr) when I pass the controls as 'ctrl'  to a method:

                cCtrl = ctrl.GetType().ToString();
                cName = ctrl.Name;                              
                if (cCtrl.Contains("ComboBox"))
                {cValue = cb.SelectedValue.ToString();}
    but there's no way I can get Descr (the displayed description) like I would with cb.Text in a non W8 Store WPF application.

    I've read about all replies like using something like selectedItem.Country.Name or var selectedItem= ComboBox_Month.SelectedValue as ComboBoxItem; but that only works with known comboxes; by passing a different combobox per call these solutions either don't compile or give a runtime error. I can see the description in the debugger with SelectedItem but I can not accsss it in any way using code like this.

    How do I solve this?

    Thursday, July 03, 2014 5:16 PM

Answers

  • If you are looking for a generic way to get the value of the property specified by the DisplayMemberPath property of the ComboBox without having to know anything about the type, you could use the dynamic keyword and reflection:

            private string GetSelectedText(ComboBox cb)
            {
                string displayMemberPath = cb.DisplayMemberPath;
                dynamic item = cb.SelectedItem;
                var propertyInfo = item.GetType().GetProperty(displayMemberPath);
                var value = propertyInfo.GetValue(item, null);
                return value.ToString();
            }

    Note that for this to work the DisplayMemberPath property of the ComboBox must have been set.

    The ComboBox does not expose the string that gets displayed in it through any property.

    Thursday, July 03, 2014 5:52 PM

All replies

  • If you are looking for a generic way to get the value of the property specified by the DisplayMemberPath property of the ComboBox without having to know anything about the type, you could use the dynamic keyword and reflection:

            private string GetSelectedText(ComboBox cb)
            {
                string displayMemberPath = cb.DisplayMemberPath;
                dynamic item = cb.SelectedItem;
                var propertyInfo = item.GetType().GetProperty(displayMemberPath);
                var value = propertyInfo.GetValue(item, null);
                return value.ToString();
            }

    Note that for this to work the DisplayMemberPath property of the ComboBox must have been set.

    The ComboBox does not expose the string that gets displayed in it through any property.

    Thursday, July 03, 2014 5:52 PM
  • Thank you for the  quick reply; yes indeed this works! I've been looking long for how to get this done. It surprises me that the ComboBox does not expose a string that gets displayed in it through any property. But anyhow, this works fortunately.
    Thursday, July 03, 2014 9:44 PM