简体   繁体   中英

How to pass parameter from code to ValueConverter

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM