白色 UI 自动化:获取 WPF DataGrid 单元格值?

发布于 2024-12-13 03:40:21 字数 1629 浏览 1 评论 0原文

我对白色项目非常陌生,我只是检查它的功能......在我的工作中,我集中处理 wpf 和 wpf 。数据网格, 当列是 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,我需要获取单元格值!!!?请帮忙!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

绮烟 2024-12-20 03:40:21

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

也就是说,如果您向 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" }));

}

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

}
轻拂→两袖风尘 2024-12-20 03:40:21

foreach(tab.Rows 中的 var r)
{
var AutomationElement = r.GetElement(SearchCriteria.ByAutomationId("moko"));

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

column.Add(label.Text);
}

此方法可能不适用于仅获取可见的所有行

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

绅刃 2024-12-20 03:40:21

我建议另一种方法。如果网格支持导出到 CSV - 请改用它。这将为您节省几个月的工作!如果我之前知道这一点就好了……

识别细胞和提取值可能会很痛苦。许多网格控件都进行 UI 虚拟化。不适合视口的单元格将不会被创建。您必须滚动网格才能强制渲染它们。

我个人使用 DevExpress 网格。他们的 CSV 导出保留了格式,因此您将获得与屏幕上显示的值完全相同的值!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文