【发布时间】:2014-03-20 05:53:34
【问题描述】:
我正在使用 wpf 数据网格。我希望数据在网格中正确并居中对齐。目前我正在使用 Style setter 属性,但没有任何东西对我有用
private void DgvInvoice_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
DataGridColumn nextColumn = DgvInvoice.ColumnFromDisplayIndex(7); // Display Index 7 is Amount Colum in the grid
DataGridRow row = (DataGridRow)DgvInvoice.ItemContainerGenerator.ContainerFromItem(new DataGridCellInfo(DgvInvoice.SelectedItem, nextColumn).Item);
DataGridCellsPresenter presenter = GetVisualChild<DataGridCellsPresenter>(row);
DataGridCell gridCell = presenter.ItemContainerGenerator.ContainerFromIndex(7) as DataGridCell;
if (gridCell != null)
{
if (d != 0)
{
gridCell.Content = String.Format("{0:#,0}", d);
}
else { gridCell.Content = string.Empty; }
Style s = new Style();
gridCell.Height = 29;
s.Setters.Add(new Setter(TextBox.TextAlignmentProperty, TextAlignment.Right));
s.Setters.Add(new Setter(TextBox.VerticalContentAlignmentProperty, VerticalAlignment.Center));
gridCell.Style = s;
}
}
【问题讨论】:
标签: wpf c#-4.0 wpfdatagrid