Search code examples
c#silverlightdatagridconvertersstaticresource

Bind to Static Resource with a Converter


I have a DataGrid and two StaticResource.

I want to bind RowStyle of DataGrid to one of two StaticResources.

RowStyle="{StaticResource {Binding Status, Converter={StaticResource MyConverter}}}"

MyConverter returns StaticResource's Key.

But I get this error:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt.


Solution

  • The Static Resource key is not a value that can be assigned Dynamically. The name of the key needs to inline in the Xaml.

    The correct approach is this:-

    RowStyle="{Binding Status, Converter={StaticResource MyConverter}}" 
    

    Where the converter that is stored against the "MyConverter" key returns a Style object. Note you could add a property of type ResourceDictionary to you converter and place you styles in that dictionary for you converter to lookup.

    In fact I have already written a converter capable of this here.