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

发布于 2024-12-03 12:12:22 字数 3996 浏览 1 评论 0原文

我在 WPF 中使用 DataGrid

我想将一些特定的字符串放入某个特定的单元格中。我们怎样才能做到这一点。

我是这样评价价值的。

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

         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>

i m using DataGrid in WPF

I want to put some specific string in some specific cell . How can we do this.

I am putting value this way .

//pPredicate is a string variable havign some value
// i m assigning in a particualr cell (3rd col)

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

the problem in top most statmetnts is the row is in edit mode the grid contains textbox while otherwise grid contains textblock. When i m putting value in textblock it does not persisting while putting value in textbox persists.

XAML part of custom 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眸中客 2024-12-10 12:12:22

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

原因是 TextBox.Text 默认绑定 TwoWay,但 TextBlock.Text 绑定 OneWay。如果您希望它起作用,您必须更改所有 TextBlock 绑定以显式使用 TwoWay ,例如

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

If I understand your question correctly then you want the changes that you make to the TextBlock/TextBox to be propagated to the underlying DataTable. This works for a TextBox but not for a TextBlock.

The reason for this is that TextBox.Text binds TwoWay by default but TextBlock.Text binds OneWay. If you want this to work you have to change all your TextBlock bindings to explicitly use TwoWay like

<TextBlock Text="{Binding Path=SomeProperty, Mode=TwoWay}"/>
迷爱 2024-12-10 12:12:22

如果您不想感兴趣 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;

if you dont want interest xaml , this code maybe work for yor need.
(xx ,yy is row and colonm number)

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;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文