<syncfusion:SfDataGrid ItemsSource="{Binding Path=ValueList}" AllowEditing="True" x:Name="sfdatagrid">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridNumericColumn MappingName="NumberOfDecimals" />
<syncfusion:GridNumericColumn MappingName="Value"
DisplayBinding="{Binding Converter={StaticResource decimalDigitConverter}}"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid> |
public class DecimalDigitConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && (value as Model) != null)
{
int decimalDegits = (value as Model).NumberOfDecimals;
if (decimalDegits == 0)
return Math.Floor((value as Model).Value);
else
return Math.Round((value as Model).Value,2);
}
return null;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return value;
}
} |
public class GridNumericColumnRendererExt : GridCellNumericRenderer
{
public override void OnInitializeEditElement(DataColumnBase dataColumn, DoubleTextBox uiElement, object dataContext)
{
if (dataColumn.GridColumn.MappingName == "Value")
{
int decimalDegits = (dataContext as Model).NumberOfDecimals;
if (decimalDegits == 0)
{
uiElement.Value = Math.Floor((dataContext as Model).Value);
uiElement.NumberDecimalDigits = 0;
}
else
{
uiElement.Value = Math.Round((dataContext as Model).Value, 2);
uiElement.NumberDecimalDigits = 2;
}
var column = dataColumn.GridColumn;
var textPadding = new Binding { Path = new PropertyPath("Padding"), Mode = BindingMode.OneWay, Source = column };
uiElement.SetBinding(Control.PaddingProperty, textPadding);
var textAlignBind = new Binding { Path = new PropertyPath("TextAlignment"), Mode = BindingMode.TwoWay, Source = column };
uiElement.SetBinding(TextBox.TextAlignmentProperty, textAlignBind);
var textWrappingBinding = new Binding { Path = new PropertyPath("TextWrapping"), Mode = BindingMode.TwoWay, Source = column };
uiElement.SetBinding(TextBox.TextWrappingProperty, textWrappingBinding);
var verticalContentAlignment = new Binding { Path = new PropertyPath("VerticalAlignment"), Mode = BindingMode.TwoWay, Source = column };
uiElement.SetBinding(Control.VerticalContentAlignmentProperty, verticalContentAlignment);
var textDecorations = new Binding { Path = new PropertyPath("TextDecorations"), Mode = BindingMode.TwoWay, Source = column };
uiElement.SetBinding(TextBox.TextDecorationsProperty, textDecorations);
}
}
} |
public override void OnInitializeEditElement(DataColumnBase dataColumn, DoubleTextBox uiElement, object dataContext)
{
base.OnInitializeEditElement(dataColumn, uiElement, dataContext);
if (dataColumn.GridColumn.MappingName == "Value")
{
int decimalDegits = (dataContext as Model).NumberOfDecimals;
if (decimalDegits == 0)
{
uiElement.Value = Math.Floor((dataContext as Model).Value);
uiElement.NumberDecimalDigits = 0;
}
else
{
uiElement.Value = Math.Round((dataContext as Model).Value, 2);
uiElement.NumberDecimalDigits = 2;
}
}
} |
<syncfusion:GridNumericColumn MappingName="Value">
<syncfusion:GridNumericColumn.DisplayBinding>
<MultiBinding Converter="{StaticResource DecimalDigitConverter}">
<Binding Path="Value"/>
<Binding Path="NumberOfDecimals"/>
</MultiBinding>
</syncfusion:GridNumericColumn.DisplayBinding>
</syncfusion:GridNumericColumn> |
public class DecimalDigitConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values[0] == DependencyProperty.UnsetValue)
return 0;
if (values[1] == DependencyProperty.UnsetValue)
return 0;
var decimalDegits = (int)values[1];
var value = (double)values[0];
var val = decimalDegits == 0 ? Math.Floor(value) : Math.Round(value, decimalDegits);
return val.ToString();
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
} |