Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
150
Alternatives to IValueConverter when Conversion needs to consider the value of a Property of a Property.
posted

Hi, I'm using XamDataGrid and binding it to a List<MyClass>, where MyClass has some public properties.   The XamDataGrid works perfectly, displaying each distinct instance of MyClass as a row, and all of the public properties as columns.

Now, I've extended each Property within MyClass to be a "Decorated" object that contains both a Value to be displayed, AND a boolean that indicates if the cell should be highlighted in some way by what ever control is displaying it. (The Decorator class is a template class that adapts a type, and handles type conversion and ToString() + contains the property bool Highlight{get;set;} ).

I've seen the examples using IValueConverter to create a setter for the Foreground property, but all the examples I've seen, add one style per column, each of which specifies a both a Converter and a Path to a specific property i.e. DataItem.Property1 or DataItem.Property2.   I'd much rather the grid worked that out for itself, as it does now (as every public property contains a Highlight flag).

I tried adding a Style that targeted the CellValuePresenter, but there doesn't appear to be a way of binding using something like Path="DataItem.*"  i.e. apply the conversion to ALL public properties. Or ".\Highlight" i.e. the currently bound item's Highlight property.

And if I try using Path="DataItem", that will get me to the MyClass instance, but I can't see how I can find out which column the XamDataGrid is currently converting (unless you can show me how to obtain that information).

If MyClass has 2 public properties, the Conversion does indeed get called twice for each row as I expected.

So, what is the correct approach to this kind of problem?   One that doesn't rely on just the Value of the property i.e. return a Red brush when the value == "Hello World", but "a property of that property" i.e. when Value.Highlight == true.

Parents
No Data
Reply
  • 54937
    Verified Answer
    Offline posted

    So just to be clear, the value of the cell is a custom type that has the actual value and the boolean Highlight property? If so then you could probably just deal with the Value property of the CellValuePresenter which would return the value of the cell and therefore your object that has the boolean property. I.e. you would bind/trigger off the Value.Highlight where the relative source is the cell value presenter. So if this were being done in a style trigger you might do something like <DataTrigger Binding="{Binding Path=Value.Highlight, RelativeSource={RelativeSource Self}}" Value="True">

Children