This is a migrated thread and some comments may be shown as answers.

Cannot select item in combox by its text value

1 Answer 240 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
David
Top achievements
Rank 1
David asked on 22 Nov 2016, 05:29 PM

Hi,

I have a combobox where I am trying to select an item by text, but it seems that I am not able to do so.

 

I have tried to use the following method

  • Find the combobox by automationId (which works): combo = app.MainWindow.Find.
                    ByAutomationId<ArtOfTest.WebAii.Controls.Xaml.Wpf.ComboBox>(comboBoxAutoId);
  • Use itemByText to select the item (which doesn´t work): combo.SelectItemByText(true, itemToSelect);

When I use combobox.selectItemByText("Text") I get a null reference exception.

 

The whole method:

public void selectItemFromComboBox(WpfApplication app, string comboBoxAutoId, string itemToSelect)
        {
            ArtOfTest.WebAii.Controls.Xaml.Wpf.ComboBox combo;
            Thread.Sleep(350);
            app.MainWindow.RefreshVisualTrees();
            combo = app.MainWindow.Find.
                ByAutomationId<ArtOfTest.WebAii.Controls.Xaml.Wpf.ComboBox>(comboBoxAutoId);
            combo.OpenDropDown(true);
             
            FrameworkElement indicator;
            for (int i = 0; i < 50; i++)
            {
                app.MainWindow.RefreshVisualTrees();
                Thread.Sleep(350);   
                indicator = app.MainWindow.Find.ByTextContent(itemToSelect);
                if (indicator != null)
                {
                    for (int j = 0; j < 50; j++)
                    {
                        Thread.Sleep(1000);
                        app.MainWindow.RefreshVisualTrees();
                        combo.Refresh();
                        combo.SelectItemByText(true, itemToSelect);
                        Debug.WriteLine("AAAAAA");
                        Thread.Sleep(500);
                        if (combo.Text.Equals(itemToSelect))
                        {
                            Thread.Sleep(350);
                            break;
                        }
                    }
                     
                }
                 
            }

Please note that I am using a framework element to ensure that the item is visible/not null.

The xaml of the combobox:

<ComboBox x:Name="cmbAttributeType"
          Grid.Row="0"
          Grid.Column="1"
          materialDesign:HintAssist.Hint="Select Type"
          Style="{StaticResource MaterialDesignFloatingHintComboBox}"
          ItemsSource="{Binding Path=AttributeTypesAvailable, ElementName=LevelAttributes}"
          SelectedItem="{Binding Path=SelectedAttributeType, ElementName=LevelAttributes}"
          IsEditable="True"
          IsReadOnly="True"
          Margin="3,0,0,0"
          AutomationProperties.AutomationId="Level_Att_Type_ComboBox"/>

 

 I tried doing this with a Framework element (opening the combobox and then clicking on the FWE), but every time I click on the FWE the comboboxlist closes and the comboboxitem is not selected. This is the same for clicking on a combobox item from an Ilist of the combobox.

Any help on this would be great.

Many thanks in advance,

David

1 Answer, 1 is accepted

Sort by
0
Nikolay Petrov
Telerik team
answered on 25 Nov 2016, 06:55 AM
Hi David,

Here are the code lines to select value from a combobox element:

myCombo.OpenDropDown(true);
// to ensure the combobox is fully open 
System.Threading.Thread.Sleep(1000);
myCombo.SelectItemByText(true, "Y");

There is no need to Refresh the combobox. Try to implement this in your code. 

I hope this will solve the issue.

Regards,
Nikolay Petrov
Telerik by Progress 
Tags
General Discussions
Asked by
David
Top achievements
Rank 1
Answers by
Nikolay Petrov
Telerik team
Share this question
or