We have a unique case where we need to display a custom-typed value in a DataGrid column. The custom type can be one of three values: Digital, Analog and Serial.
- Digitals represent either '0' or '1' in the model
- Analogs are integers between '0' and '255'
- Serials are simply strings
For analogs or serials, we simply want to display the value as-is in the column of a DataGrid and for those types, the values are editable, but for digitals which are stored as '0' and '1' in the model, we want to display them as the string 'True' or 'False' and they are not editable.
Here's an example of the logic we need in the Convert
method:
// In this example, 'rawValue' is the value as represented in the model
// ValueType is an enum of Digital, Analog or Serial
return (valueType != ValueType.Digital)
? rawValue
: (rawValue == 0)
? "False"
: "True";
We then need the reverse for the ConvertBack
method.
The issue we're running into is passing in valueType
to the converter to be used in both the Convert
and ConvertBack
methods. You can't use Parameter
for this since you can't set a binding on Parameter. Likewise, you can't simply use a MultiValueConverter
either as ConvertBack
isn't handed that value but rather expects you to return it.
So how would one pass in ValueType to both the Convert and ConvertBack methods of the converter?
I'm leaning towards exposing a new property on the converter and binding to that, but I'm not sure if that binding is only evaluated once, when the converter is parsed.