Question

I have a datagrid for my customer data. My customer entity has a collection of notes exposed.

I need a method of displaying an image in a column based on the notes status, if any of my notes have a status of "warning", then display a warning image otherwise a normal status image.

Is this do-able?

Was it helpful?

Solution 2

I added a read only [NotMapped] property to my Customer entity(am using Entity Framework 4) which returned a boolean and then bound an Image inside DataGridTemplateColumn to this and set the source using a value converter:

Entity

[NotMapped]
public bool ShowWarning 
{
    get
    {
        if (this.AuditableNotes != null && this.AuditableNotes.Count(an => an.Warning) > 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

XAML

<DataGridTemplateColumn
            Header="Status">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Image x:Name="MyImage" Source="{Binding ShowWarning, Converter={StaticResource notesStatusConverter}}" Width="25" Height="20"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

ValueConverter

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    if (value != null && (bool)value == true)
    {
        return "/Assets/Images/symbol_error.png";
    }
    else
    {
        return "/Assets/Images/symbol_information.png";
    }

}

OTHER TIPS

Yes there are multiple ways to accomplish this.

If you have a customer ViewModel, then just expose a property that tells you whether the particular customer has a warning status in their notes collection, and then use that to determine whether to show the image or not.

Another option would be to use a ValueConverter that takes in your notes collection and then determines whether to show the image.

I am sure that there are other approaches, but these are the ones that popped into my head.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top