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

问题描述 投票:0回答:2

我在WPF中使用DataGrid

我想在一些特定的单元格中放置一些特定的字符串。我们应该怎么做。

我这样做是有价值的。

//谓词是一个字符串变量,有一些值//我在特定的单元格中分配(第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;
        }

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

XAML是自定义DataGrid的一部分

            <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>
c# .net wpf datagrid
2个回答
1
投票

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

原因是TextBox.Text默认绑定TwoWayTextBlock.Text绑定OneWay。如果你想要这个工作你必须改变你所有的TextBlock绑定明确使用TwoWay

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

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; else cell.Background = Brushes.Pink;

© www.soinside.com 2019 - 2024. All rights reserved.