访问文件WPF后面的代码中的DataGrid单元格

时间:2015-04-13 18:39:36

标签: c# wpf datagrid

我试图在遍历所有行时访问wpf中的Datagrid单元格值。我想得到每行第一列的值。我尝试了很多东西但是没有成功。请帮助

以下是我正在尝试的内容

 var dg = productFilterResult.ProductsGrid;
                for (int i = 0; i < dg.Items.Count; i++)
                {
                    DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                    var cellContent = dg.Columns[1].GetCellContent(row);
                    var x = (ContentPresenter)cellContent;
                    var y = x.Content;
                }

这是我的xaml文件:                   

                <DataGridTemplateColumn Header="Id" Visibility="Collapsed" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Id}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Index"  Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Name="IndexCell" Text="{Binding RelativeSource={RelativeSource AncestorType=DataGridRow}, Converter={StaticResource RowToIndexConvertor}}"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Name" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Name}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Description" Width="*" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Path=Description}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Update" Width="Auto" MinWidth="200" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image  x:Name="EditImg" Style="{DynamicResource EditImg}" Source="/CPOSApplication;component/Icons/edit.png" Tag="{Binding}" MouseDown="Edit_Click"></Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Delete" Width="Auto" IsReadOnly="True">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Image  x:Name="DeleteImg" Style="{DynamicResource DeleteImg}" Source="/CPOSApplication;component/Icons/delete.png" Tag="{Binding}" MouseDown="Delete_Click"></Image>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

2 个答案:

答案 0 :(得分:3)

请尝试以下代码。

 public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
    {
        if (row != null)
        {
            DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);

            if (presenter == null)
            {
                grid.ScrollIntoView(row, grid.Columns[column]);
                presenter = GetVisualChild<DataGridCellsPresenter>(row);
            }

            DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
            return cell;
        }
        return null;
    }

    public static T GetVisualChild<T>(Visual parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }

参考链接http://svgvijay.blogspot.com/2013/01/how-to-get-datagrid-cell-in-wpf.html

答案 1 :(得分:2)

这就是我的工作方式

 var dg = productFilterResult.ProductsGrid;
                for (int i = 0; i < dg.Items.Count; i++)
                {
                    DataGridRow row = (DataGridRow)dg.ItemContainerGenerator.ContainerFromIndex(i);
                    var cellContent = dg.Columns[1].GetCellContent(row);
                    var cellContentPresenter = (ContentPresenter)cellContent;
                    DataTemplate editingTemplate = cellContentPresenter.ContentTemplate;
                    TextBlock CellTextBox = editingTemplate.FindName("IndexCell", cellContentPresenter) as TextBlock;
                    string CellValue = CellTextBox.Text;
                    var BindedModel = cellContentPresenter.Content;
                }