0

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?

CheGuevarasBeret
  • 1,364
  • 2
  • 14
  • 33

2 Answers2

0

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.

Brent Stewart
  • 1,830
  • 14
  • 21
  • Can I bind a collection of notes to a DataGridTemplateColumn? I have tried but value passed into my ValueConverter is always null. – CheGuevarasBeret Feb 25 '13 at 20:24
  • I just checked and yes you can bind to a collection and have it passed to a ValueConverter. I am not sure why you are having issues. If you continue to have problems doing it, then start a new question on SO as this is not the proper place to try to solve it. Also please make sure to upvote or mark as answer on answers that are helpful. – Brent Stewart Feb 26 '13 at 16:02
0

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";
    }

}
CheGuevarasBeret
  • 1,364
  • 2
  • 14
  • 33