更改数据网格单元格的小数位数
本文关键字:小数 单元格 网格 数据 数据网 | 更新日期: 2023-09-27 18:26:51
我有一个绑定到可观察集合的datagridId。我想在可观察集合中存储正确的值(使用所有小数),但我想在数据网格中看到更少的小数。所以我试过了在WPF中以编程方式更改DataGrid单元格值以及一些类似的其他人。
最后,我需要的是在触发事件时更改数据网格的值。
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);
}
所以上面的rowcontainer不是null,但当我试图获取单元格值时,我会得到一个null异常。特别是:
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;
}
上面的演示者是空的,在输入if.后也是空的
我怎样才能让它工作?Thanx
---添加----除了前面提到的问题,我如何进行单向绑定?我已经设置了与的数据网格绑定
dtgResults.ItemsSource = easyRunData.olstMeasures;
但现在我只想更改dtgResults的小数位数,而不是可观察的集合值。
更改DataGridCell值的最简单方法是使用TextBlock
:的Loaded event handler
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Area, Mode=TwoWay, UpdateSourceTrigger=LostFocus}" Loaded="TextBlock_Loaded"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
private void TextBlock_Loaded(object sender, RoutedEventArgs e)
{
TextBlock tb = ((TextBlock)sender);
// do anything with textblock
if (tb.Text == 10)
{
tb.Background = Brushes.Plum;
}
}
如果是AutogenerateColumns = true
,那么我们需要处理DataGridCell的Loaded事件。
<DataGrid.Resources>
<Style TargetType="DataGridCell">
<EventSetter Event="DataGridCell.Loaded" Handler="DataGridCell_Load"/>
</Style>
</DataGrid.Resources>
private void DataGridCell_Load(object sender, RoutedEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell.Column.Header.ToString() == "MyColumn")
((TextBlock)cell.Content).Text = ...do something... ;
/* to get current row and column */
DataGridColumn col = cell.Column;
Dgrd2.CurrentCell = new DataGridCellInfo(cell);
DataGridRow row = (DataGridRow)Dgrd2.ItemContainerGenerator.ContainerFromItem(Dgrd2.CurrentItem);
}
如果在代码后面为DataGrid
生成列,则可以编写这样的内容。Xaml部分。
<DataGrid Name="dgTest" AutoGenerateColumns="False">
</DataGrid>
ValueConverter
代码。在examle中,我使用了3位数的舍入,但您可以将其作为ConverterParameter
传递并改进代码。
public sealed class DecimalConverter : IValueConverter
{
public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
double result = 0;
if ( value is double )
result = Math.Round( ( double )value, 2 );
return result;
}
public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
{
throw new NotImplementedException();
}
}
正在初始化代码。
DataGridTextColumn valColumn = new DataGridTextColumn();
Binding valBinding = new Binding( "SomeVal" );
valBinding.Converter = new DecimalConverter();
valColumn.Binding = valBinding;
dgTest.Columns.Add( valColumn );
dgTest.ItemsSource = Objects;
我的测试Observable集合名为Objects
,包含具有双重属性SomeVal
和实现INotifyPropertyChanged
行为的对象。
private double _someVal;
public double SomeVal
{
get { return _someVal; }
set { _someVal = value; NotifyPropertyChanged( "SomeVal" ); }
}
更新典型的INotifyPropertyChanged
实现。
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
protected void NotifyPropertyChanged( String info )
{
if ( PropertyChanged != null )
{
PropertyChanged( this, new PropertyChangedEventArgs( info ) );
}
}