How to pass parameter from code to ValueConverter

Go To


I have an IValueConverter class that is used to change the background color of a datagrid cell in case the value in the cell itself is greater than 0 (not important).

Now I want to create several instances of the window that contains this datagrid. Depending on some values defined in the Window instance the IValueConverter shall color the background of a cell of the datagrid or not. So the IValueConverter shall do a different job depending on the instance of the Window class.

The problem now is: I can't pass ConverterParameter in XAML because that parameter would be a fix one for all instances of the window. I thought about passing the relevant information to the IValueConverter class programmatically (that is required) but how can I access the instance of the IValueConverter class? My current solution: I put some static variables into the IValueConverter class so I can access them from the instance of the Window class. But this solution is a bad one because the variables are static and so the ValueConverter behaves the same way in each of the window instances.

Does anyone know how to access the IValueConverter instance programmatically from the Window instance? Thanks!

2012-04-04 20:15
by manton


Since you cannot databind to the ConverterParameter, you can one of two things (also see this SO question):

  1. Add a dependency property to your view model and do a multi-value converter
  2. Pass the entire view model into your binding expression
2012-04-04 20:20
by Philipp Schmid
@Phillip Schmid Unfortunately I don't know how to manage your second proposal (I don't know the syntax). How can I pass the entire view model into my binding expression? Does that mean I can access the view model (i.e. the DataContext) in the ValueConverter then? That would be a good way.. - manton 2012-04-04 20:38
Value={Binding} without any path means that you pass the entire DataContext. Also if you add a converter, you'll pass the DataContext to the Converte - pluka 2012-04-04 20:53