how to get the value of a selecteditem in a combo box

Sep 30, 2010 at 6:36 PM

I have a combo box and i want to check the value it is currently holding.

combobox.selectedItem.text returns empty

combobox.selecteditemtext also returns empty

how should i do it.

 

please help

Oct 1, 2010 at 9:52 AM

Hi

in most cases this value is visible after click on the combobox.

 

I'm not sure but it seams that white don't use the selection pattern to get this value

 

without opening the comoboBox=>

 

 

/// <summary>
/// Gets the selected item text from combo box.
/// </summary>
/// <param name="combobox">The combobox.</param>
/// <returns></returns>
public static string GetSelectedItemTextFromComboBox(AutomationElement combobox)
{
  AutomationPattern automationPatternFromElement = GetSpecifiedPattern(combobox, "SelectionPatternIdentifiers.Pattern");

  SelectionPattern selectionPattern = combobox.GetCurrentPattern(automationPatternFromElement) as SelectionPattern;

  if (selectionPattern != null)
  {
	AutomationElement[] listItems = selectionPattern.Current.GetSelection();

	string selectedItemText = listItems[0].Current.Name;

	return selectedItemText;
  }

  return null;
}

/// <summary>
/// Gets the specified pattern.
/// </summary>
/// <param name="element">The element.</param>
/// <param name="value">The value.</param>
/// <returns></returns>
public static AutomationPattern GetSpecifiedPattern(AutomationElement element, string value)
{
  AutomationPattern[] supportedPattern = element.GetSupportedPatterns();

  AutomationPattern automationValuePattern = null;
  foreach (AutomationPattern pattern in supportedPattern)
  {
	if (pattern.ProgrammaticName == value)
	{
	  automationValuePattern = pattern;
	  break;
	}
  }
  return automationValuePattern;
}

 

Throndorin

Oct 1, 2010 at 7:55 PM

Throndrin,

 

Thank alot for you reply and the code. I chose to do it the first work around you mentioned. I click on the combo box and then get the value of the selected item and it returns the correct value. This is a bug in White i hope they will fix it in the future release.

 

Thanks

Coordinator
Oct 10, 2010 at 4:19 AM

can you provide some code sample to reproduce this problem.

Oct 11, 2010 at 8:14 AM
Edited Oct 11, 2010 at 8:15 AM

Hi

I can't but you can easly build an application:

 

use WPF Datagrid wich contains a bool value changeable with a combobox, but the field is impllemented one way, so you can't change the value.

(We have 2 data grids in different regions, in one you can change in the other not)

 

=> edit: much more easy :) build application with combobox but ist visible and disabled.

 

problem, you can't click on the combobox to show the elements. WPFCombobox.GetSelectedItem or selectedItemText are bot "null"

 

Throndorin

Apr 15, 2011 at 8:07 AM

 

Thanks Throndorin - that saved me a bunch of time. :)