开发者

How to put data in WPf Data Grid specific cell

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 sta开发者_运维知识库tmetnts 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>


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}"/>


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;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜