4

我对白色项目非常陌生,我只是检查它的功能......在我的工作中,我集中处理 wpf 和 datagrids,当列是 DataGridTemplateColumn 时,我无法获得 datagrid 单元格的值。

它不仅适用于 DataGridTemplateColumn,它适用于所有列类型。

我的数据网格是:

    <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>

我的测试是:

           [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 :(
    }

但单元格计数始终 = 0,我需要获取单元格值!!!?请有任何帮助!

4

3 回答 3

1

我不喜欢这个答案,并想找到一种更好的方法来做到这一点..

也就是说,如果您向 ListViewRow 项询问网格中的元素,则可以使用 UiAutomationElement 并自己创建该元素的白色版本。

[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" }));

}
于 2011-11-05T21:43:54.587 回答
0

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!

于 2014-08-22T16:41:32.887 回答
0

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

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

列。添加(标签。文本);}

作为警告,此方法可能不适用于仅获取所有行可见的行

于 2013-11-22T14:05:42.027 回答