Need Value Converter to reference databound object instance
-
04-07-2021 - |
سؤال
So I have an object with multiple attributes that I have bound to my GUI using WPF. Most of the GUI binds directly to properties in my object instance. Some of the GUI elements need to be processed via a ValueConverter first however, as they rely indirectly on an attribute in the bound object. I can create the valueconverter, but was uncertain how to reference the databound object in order to pull the right property values for the conversion.
In pseudo-code I want to do this:
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
parameter = Databoundobject.EngineRating;
double weight = EngineList.selfInstance.WeightList[parameter];
}
I can find examples that don't reference a databound source in the calling gui, but none that do. Does anyone know how I can accomplish this?
المحلول
The value
parameter in the Convert()
method will hold that raw value from the data-bound source.
Therefore, if you use the converter in a binding to the EngineRating
property, value
will hold the value you're looking for. (You'll need to cast it back to the original type)