How can I do the Binding of my Converter with the DataGrid in Code, and how can I bind on ColumnGeneration?
this doesn't work for me:
col.DisplayBinding = new Binding("Vals[" + key.Key + "]", BindingMode.OneWay,
new GridCurrencyConverter());
Here is my code:
public class GridCurrencyConverter : IValueConverter
{
//check data and replace "-1" with "-"
public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo)
{
try
{
string tmp = Helper.DataGridValueConverter(value.ToString());
if (tmp != "-" && (tmp.Contains(".") || tmp.Contains(",")))
{
double val = System.Convert.ToDouble(value);
val = Math.Round(val);
return Helper.ConvertValue(val.ToString());
}
return tmp;
}
catch (Exception)
{
return value;
}
}
//keep current data
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo)
{
return value;
}
}
private SfDataGrid CreateDataGrid(List<DynamicData<string, object>> dynData)
{
List<Dictionary<string, string>> objList = CreateGridItems(dynData);
ViewModel vm = new ViewModel();
vm.dataGridDetails = new ObservableCollection<DataGridItem>();
foreach(Dictionary<string, string> di in objList)
{
vm.dataGridDetails.Add(new DataGridItem() { Vals = di });
}
dGrid = new SfDataGrid();
dGrid.ItemsSource = vm.DataGridDetails;
dGrid.ColumnSizer = ColumnSizer.None;
dGrid.AlternationCount = 2;
dGrid.GridStyle.AlternatingRowColor = Color.FromHex("#d3d3d3");
dGrid.AutoGenerateColumns = false;
dGrid.AllowSorting = true;
foreach (KeyValuePair<string, string> key in objList[0])
{
GridTextColumn col = new GridTextColumn();
col.MappingName = "Vals[" + key.Key + "]";
col.HeaderText = SprocEvaluator.TagConverter(key.Key);
col.Width = 120d;
//this doesn't work: Cannot convert from GridCurrencyConverter to Xamarin.Forms.IValueConverter????
col.DisplayBinding = new Binding("Vals[" + key.Key + "]", BindingMode.OneWay, new GridCurrencyConverter());
dGrid.Columns.Add(col);
}
return dGrid;
}
Appreciate any help.
Thanks