1

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.

Mark A. Donohoe
  • 28,442
  • 25
  • 137
  • 286

2 Answers2

2

I don't think this is possible to achieve with just a converter, but you should be able to use DataTemplate functionality to achieve pretty much the same result.

  1. Use DataGridTemplateColumn
  2. Define separate DataTemplate for each ValueType. Each template would have its own value converter that would only need to handle values of one ValueType which would eliminate the need to pass the ValueType into the Convert method.
  3. Use a custom DataTemplateSelector to pick a proper template based on the type of the value.
  • Very familiar with DataTemplateSelectors and have written many custom versions of it. But I'm not seeing how that helps here. Wouldn't you also have to define the Editing template for those items? Digitals aren't editable, so I'm not sure how you'd go about doing that. Would you just return 'Null' for that data type? Have you actually tried this, or are you just doing a thought experiment? – Mark A. Donohoe Nov 10 '15 at 03:43
  • @MarqueIV, yes, you would need to define Editing template and a selector as well. As for disabling editing for Digital type, I would do that using **IsReadOnly** property. And yes, I would return **null** from editing selector in this case. – Michael Domashchenko Nov 10 '15 at 13:48
0

I would create a new object representing the custom value.

public class MyCustomValue
{
    public ValueType Type { get; set; }
    public object RawValue { get; set; }
} 

On DataGrid's DataContext:

public class MyObjectBoundToDataGrid
{
    public MyCustomValue Value { get; set;}   
}

You can now easily convert your Value.

tgpdyk
  • 1,203
  • 9
  • 13
  • Not following this. We already have an object with both ValueType and RawValue on it--the existing data object--so I'm not seeing how this solves our issue. Can you please elaborate more? – Mark A. Donohoe Nov 10 '15 at 03:46
  • Well, I thought your value is not an object yet. And this makes me think why it is needed to have a converter if you have an object already. I will upvote Michael Domashchenko's answer since I think that is the way to go. – tgpdyk Nov 10 '15 at 06:56