简体   繁体   English

在文件WPF的CodeBehind中访问DataGrid单元

[英]Access DataGrid Cell in CodeBehind File WPF

I am trying to access Datagrid cell value in wpf while iterating through all its row.I want to get value of first column of every row.I have tried so many things but couldn't succeed.Please help 我试图在wpf中遍历其所有行时访问Datagrid单元值。我想获取每一行的第一列的值。我已经尝试了很多事情但无法成功。请帮助

Here is what I am trying 这是我正在尝试的

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

This is my xaml file: 这是我的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>

Try the below code. 试试下面的代码。

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

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

This is how it worked for me 这就是我的工作方式

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM