datagrid – programmatically binding images to silverlight grid cell based on condition – Education Career Blog

how can i programmatically binding images to silverlight datagrid cell of a particular column based on the datasource values

,

Use an implementation of IValueConverter that I blog about here. I’ll assume for the moment (your question lacks detail so I’ll make some up) that you have some kind of property that exposes an enum of a finite set of states.

In your case the value converter will contain a ResourceDictionary of BitmapImage entries:-

        <local:StringToObjectConverter x:Key="StatusToIcon">
            <ResourceDictionary>
                <BitmapImage UriSource="/Assets/State1.png" x:Key="State1" />
                <BitmapImage UriSource="/Assets/State2.png" x:Key="State2" />
                <BitmapImage UriSource="/Assets/UnknownState.png" x:Key="__default__" />
            </ResourceDictionary>
        </local:StringToObjectConverter>   

In the template for your cell you would use:-

<Image Source="{Binding State, Converter={StaticResource StatusToIcon}}" />

Leave a Comment