如何将数据放入 WPf 数据网格特定单元格
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我正确理解您的问题,那么您希望对
TextBlock
/TextBox
所做的更改传播到底层DataTable
。这适用于TextBox
,但不适用于TextBlock
。原因是
TextBox.Text
默认绑定TwoWay
,但TextBlock.Text
绑定OneWay
。如果您希望它起作用,您必须更改所有TextBlock
绑定以显式使用TwoWay
,例如If I understand your question correctly then you want the changes that you make to the
TextBlock
/TextBox
to be propagated to the underlyingDataTable
. This works for aTextBox
but not for aTextBlock
.The reason for this is that
TextBox.Text
bindsTwoWay
by default butTextBlock.Text
bindsOneWay
. If you want this to work you have to change all yourTextBlock
bindings to explicitly useTwoWay
like如果您不想感兴趣 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;