【发布时间】:2015-12-14 10:32:37
【问题描述】:
我有一个绑定到可观察集合的 datagriId。我想在可观察集合中存储正确的值(所有小数),但我想在数据网格中看到更少的小数。 所以我尝试了这个 Change DataGrid cell value programmatically in WPF 还有一些类似的。
最后我需要的是在触发事件时更改数据网格的值。
private void Datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
{
DataGridRow row = e.Row;
var pePCDmise = row.Item as PcDmisData.Measures;
DataGridRow rowContainer = dtgResults.GetRow(0);
DataGridCell d = dtgResults.GetCell(rowContainer, 3);
}
所以上面的行容器不是空的,但是当我尝试获取单元格值时,我得到一个空异常。 特别是:
public static DataGridCell GetCell(this DataGrid grid, DataGridRow row, int column)
{
if (row != null)
{
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
if(presenter == null)
{
grid.ScrollIntoView(row, grid.Columns[column]);
presenter = GetVisualChild<DataGridCellsPresenter>(row);
}
DataGridCell cell = (DataGridCell)presenter.ItemContainerGenerator.ContainerFromIndex(column);
return cell;
}
return null;
}
上面的presenter为null,输入if后也为null。
我怎样才能让它工作? 谢谢
---添加---- 除了上述问题外,我如何进行单向绑定?我已经用
设置了数据网格绑定 dtgResults.ItemsSource = easyRunData.olstMeasures;
但现在我只想更改 dtgResults 的小数位数,而不是可观察的集合值。
【问题讨论】:
-
也许最好为这一行写
ValueConverter,这样会舍入绑定值的数目 -
@bars222 怎么样?你能提供一个实际的例子吗?我正在使用代码隐藏,并且如上所述设置了 dtgResults.ItemsSource = easyRunData.olstMeasures;。我认为从 Datagrid_LoadingRow 工作会更好,因为并非所有列都包含要正确设置的数字。
标签: c# wpf data-binding datagrid cell