White UI Automation : Get WPF DataGrid cell value?

4.9k Views Asked by At

I'm very new to white project, and I wast just checking its features... In my work, I deal intensively with wpf & datagrids, I couldn't get the value of datagrid cell when the column is DataGridTemplateColumn.

It's not for DataGridTemplateColumn only, It's is for all column types.

my datagrid was as:

    <my:DataGrid AutoGenerateColumns="False" Margin="25,28,42,34" Name="dataGrid1" >
        <my:DataGrid.Columns>
            <my:DataGridTemplateColumn Header="Header"  x:Name="koko" Width="200">
                <my:DataGridTemplateColumn.CellTemplate >
                    <DataTemplate >
                        <TextBlock Name="moko" Text="{Binding col1, Mode=OneWay,Converter={StaticResource fataGridHighlightConverter }}" ></TextBlock>

                    </DataTemplate>
                </my:DataGridTemplateColumn.CellTemplate>
                <my:DataGridTemplateColumn.CellEditingTemplate>
                    <DataTemplate>
                        <Grid x:Name="myEditGrid" Loaded="myEditGrid_Loaded">
                        </Grid>
                    </DataTemplate>
                </my:DataGridTemplateColumn.CellEditingTemplate>
            </my:DataGridTemplateColumn>
        </my:DataGrid.Columns>
    </my:DataGrid>

and my Test was:

           [Test]
    public void TestDatagrid5()
    {
        var tab = _win.Get<ListView>(SearchCriteria.ByAutomationId("dataGrid1"));
        var count = tab.Rows.Count;
        var row = tab.Rows[1];
        ListViewCell x = row.Cells[1]; //Always count = 0 :(
    }

but the cell count is always = 0, I need to get cell value !!!? any help please!

3

There are 3 best solutions below

0
On

I don't like this answer and would like to find a better way of doing this..

That said if you ask the ListViewRow item for the element in your grid you can then take the UiAutomationElement and create the White version of that element your self.

[Test]
public void TestDatagrid5()
{
  var tab = _win.Get<ListView>(SearchCriteria.ByAutomationId"dataGrid1"));

  var column = new List<string>();

  foreach (var r in tab.Rows)
  {
    var automationElement = r.GetElement(SearchCriteria.ByAutomationId("moko"));

    var label = new Label(automationElement, new NullActionListener());

    column.Add(label.Text);
  }

  Assert.That(column, Is.EquivalentTo(new[] { "what", "is", "expected" }));

}
0
On

I'd suggest another way around. If the grid supports export to CSV - use it instead. That will save you man months of work! If only I knew that before...

Identifying cells and extracting values can be painful. Many grid controls do UI virtualization. The cells which do not fit the viewport won't be created. You'll have to scroll the grid to force their rendering.

Personally I use the DevExpress grid. Their CSV export preserves the formatting, so you'll get the values exactly as they appear on the screen!

0
On

foreach (var r in tab.Rows) { var automationElement = r.GetElement(SearchCriteria.ByAutomationId("moko"));

var label = new Label(automationElement, new NullActionListener());

column.Add(label.Text); }

As a warning this method may not work for getting all the rows just the ones visible