【问题标题】:Change DataGrid cell value programmatically in WPF在 WPF 中以编程方式更改 DataGrid 单元格值
【发布时间】:2012-08-28 17:01:17
【问题描述】:

我有一个DataGrid 视图,它在运行时通过列表填充其内容。 我想更改(例如)行:2 和列:3 中的单元格值。

我该怎么做?

【问题讨论】:

    标签: wpf datagrid


    【解决方案1】:

    没那么复杂。

    你需要辅助函数;

    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;
    }
    
    public static DataGridRow GetSelectedRow(this DataGrid grid)
    {
        return (DataGridRow)grid.ItemContainerGenerator.ContainerFromItem(grid.SelectedItem);
    }
    public static DataGridRow GetRow(this DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
    
    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 DataGridCell GetCell(this DataGrid grid, int row, int column)
    {
        DataGridRow rowContainer = grid.GetRow(row);
        return grid.GetCell(rowContainer, column);
    }
    

    使用 yourDataGrid.GetCell(5, 3).Content = "whatever you want";

    【讨论】:

    • 这是个玩笑,对吧?以前只需要一行就可以在 Winforms 中获取/设置 Datagridview 的值!
    • @bodycountPP;不幸的是没有。虽然,事实是,你永远不应该以编程方式更改单元格值 - 这不是 WPF 的用途,WPF 不是 winforms 的替换或升级,它是一项新的创新。查看 DataGridView 和 MVVM 以了解在商业应用程序中应该如何完成。
    • GetVisualChild 始终返回 null。为什么会这样?
    • 这行得通,但是如果您更改了一堆值,一旦您离开,这些值就会变回原来的形式。如果您选择单元格并滚动然后向后滚动,它将保留您分配的值,但其他单元格将恢复为原始值......奇怪。
    【解决方案2】:

    例如,如果您的网格上有 TextBox

    <TextBox  Grid.Row="2" Grid.Column="3" Text="{Binding ...}"/>
    

    完整样本

    <Grid x:Name="RootElement">
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
          </Grid.ColumnDefinitions>
    
          <Grid.RowDefinitions>
            <RowDefinition/>
            <RowDefinition/>
            <RowDefinition/>
          </Grid.RowDefinitions>
    
          ...
          <TextBox  Grid.Row="2" Grid.Column="3" Text="{Binding ...}"/>
          ...
        </Grid>
    

    您可以使用这些助手;你有这个链接http://techiethings.blogspot.fr/2010/05/get-wpf-datagrid-row-and-cell.html

    我添加了代码,以便在站点关闭的情况下将代码保存在 stackoverflow 上

    public static DataGridRow GetRow(this DataGrid grid, int index)
    {
        DataGridRow row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        if (row == null)
        {
            // May be virtualized, bring into view and try again.
            grid.UpdateLayout();
            grid.ScrollIntoView(grid.Items[index]);
            row = (DataGridRow)grid.ItemContainerGenerator.ContainerFromIndex(index);
        }
        return row;
    }
    
    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 DataGridCell GetCell(this DataGrid grid, int row, int column)
    {
        DataGridRow rowContainer = grid.GetRow(row);
        return grid.GetCell(rowContainer, column);
    }
    

    【讨论】:

    • 我想以编程方式进行,每次选择不同的单元格时,我的选择都不是静态的,我该怎么做?
    • 一切正常,但是当我向上或向下移动 gw 时,数据会消失。有什么问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 2013-09-29
    • 1970-01-01
    • 2010-12-10
    • 2016-03-29
    • 2017-01-26
    相关资源
    最近更新 更多