如何将数据放入WPf数据网格特定单元格中

时间:2011-09-07 12:55:09

标签: c# .net wpf datagrid

我在WPF中使用DataGrid

我想在某个特定的单元格中添加一些特定的字符串。我们怎么能这样做。

我正在以这种方式付出代价。

// pPredicate是一个字符串变量,有一些值 //我在特定单元格中分配(第3列)

         if (GetCell(3).Content is TextBlock)
            {
                ((TextBlock)(GetCell(3).Content)).Text = pPredicate;
            }
            else // TextBox
            {
                ((TextBox)(GetCell(3).Content)).Text = pPredicate;
            }



private DataGridCell GetCell(int column)
        {
            DataGridRow rowContainer = GetRow();

            if (rowContainer != null)
            {
                DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(rowContainer);

                // Try to get the cell but it may possibly be virtualized.
                DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                if (cell == null)
                {
                    // Now try to bring into view and retreive the cell.
                    customDataGrid.UCdataGridView.ScrollIntoView(rowContainer, customDataGrid.UCdataGridView.Columns[column]);
                    cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
                }
                return cell;
            }
            return null;
        }

       private DataGridRow GetRow()
        {
            DataGridRow row = (DataGridRow)customDataGrid.UCdataGridView.ItemContainerGenerator.ContainerFromIndex(_currentRowIndex);
            if (row == null)
            {
                // May be virtualized, bring into view and try again.
                customDataGrid.UCdataGridView.UpdateLayout();
                customDataGrid.UCdataGridView.ScrollIntoView(customDataGrid.UCdataGridView.Items[_currentRowIndex]);
                row = (DataGridRow)customDataGrid.UCdataGridView.ItemContainerGenerator.ContainerFromIndex(_currentRowIndex);
            }
            return row;
        }

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

最顶级的数据中的问题是行处于编辑模式,网格包含文本框,否则网格包含文本块。当我在文本块中放置值时,它在文本框中保留值时不会持久存在。

自定义DataGrid的XAML部分

                             

            <WPFtoolkit:DataGridTextColumn x:Name="dgName" Binding="{Binding Path=Name}" Header="Name" MinWidth="100" Visibility="Collapsed"/>


            <WPFtoolkit:DataGridTextColumn x:Name="dgPredicates" Binding="{Binding Path=Predicate}" Header="Predicate" MinWidth="100"
                                           Visibility="Collapsed"/>



            <WPFtoolkit:DataGridTemplateColumn Header="Delete" IsReadOnly="True"
                                                                Visibility="Collapsed" MaxWidth="80" Width ="*">
                <WPFtoolkit:DataGridTemplateColumn.CellTemplate>
                    <DataTemplate>
                        <Image Source="../images/Delete.png" Height="15" Width="15"/>
                    </DataTemplate>
                </WPFtoolkit:DataGridTemplateColumn.CellTemplate>
            </WPFtoolkit:DataGridTemplateColumn>

            <WPFtoolkit:DataGridTextColumn Binding="{Binding Path=TreeType}" Header="Tree Type" Width="50"
                                       Visibility="Collapsed"/>
        </WPFtoolkit:DataGrid.Columns>
    </WPFtoolkit:DataGrid>

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,那么您希望对TextBlock / TextBox所做的更改传播到基础DataTable。这适用于TextBox,但不适用于TextBlock

原因是TextBox.Text默认绑定TwoWay,但TextBlock.Text绑定OneWay。如果您希望这项工作正常,您必须更改所有TextBlock绑定,以明确使用TwoWay,如

<TextBlock Text="{Binding Path=SomeProperty, Mode=TwoWay}"/>

答案 1 :(得分:0)

如果你不想要感兴趣的xaml,这段代码可能适用于你的需要。 (xx,yy是行和冒号)

DataGridCell cell =(YourDgName.Columns [XX] .GetCellContent(DgCagrilar.Items [YY]))。Parent s DataGridCell;                         if(cell.Background == Brushes.Pink)                             cell.Background = Brushes.Plum;                         其他                             cell.Background = Brushes.Pink;