views:

249

answers:

1

I create DataGrid Columns with Binding (where i is a Int value):

dataGrid.Columns.Add(new DataGridTextColumn
{
   Header = i.ToString(),
   Binding = CreateBinding(i),
});

private Binding CreateBinding(int num)
{
   Binding bind = new Binding(string.Format("[{0}]", num));         
   bind.Converter = new CellValueConverter();
   return bind;
}

In the CreateBinding method I have an access to bind.Converter property.
I need to call Converter.Convert() method in some handler, but there is no Converter property when I try to access it:

(dataGrid.Columns[clm] as DataGridTextColumn).Binding."no Converter property!"

How can I get my CellValueConverter which was created for particular Column?

+3  A: 

This is because the Binding property on DataGridBoundColumn (and DataGridTextColumn) actually returns a BindingBase instance, not a Binding. BindingBase doesn't support converters.

You should be able to try to cast:

var binding = (dataGrid.Columns[clm] as DataGridBoundColumn).Binding as Binding;
if(binding != null)
{
    IValueConverter converter = binding.Converter; // Will work here
 }
Reed Copsey