2

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!

abatishchev
  • 92,232
  • 78
  • 284
  • 421
manton
  • 423
  • 1
  • 6
  • 25

1 Answers1

2

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
Community
  • 1
  • 1
Philipp Schmid
  • 5,636
  • 5
  • 41
  • 61
  • @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 Apr 04 '12 at 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 Converter – pluka Apr 04 '12 at 20:53