I have a dataset with a few int fields that need to display as images. The value for these fields is 1,2,3,4, or 5. Based on the value of that field I want to display one of five images in the cell.
You can do this in two ways:
You can create a trigger or a binding, which will check the Field.Name property so that you know which field it is, something like this in pseudo code :
Binding - Path=Field.Name, RelativeSource-FindAncestor-CellValuePresenter (or TemplatedParent).
The other way is to set an x:key property to the style and set this style to the CellValuePresenterStyle property of the fields that you want.
Thanks for your help. I think I'm almost there. Is there a way inside of the template to see which column I'm currently working with. I have 3 or 4 columns that need this logic and I'm trying to see if I can use one template instead of creating a template for each data point that needs this logic.
Hello,
What you need to do is to retemplate the CellValuePresenter and put an Image element inside it.
You can assign this style to the specific field's CellValuePresenterStyle property of the FieldSettings.
In the control template, you can create DataTriggers to bind any cell's value to the source property of the image. For example, the following code snippet changes the image depending on the Id field of the XamDataGrid.
<Style x:Key="imageStyle" TargetType="{x:Type igDP:CellValuePresenter}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Image x:Name="image" Width="20" Height="20" Source="blue.png"/>
<ControlTemplate.Triggers>
<DataTrigger Binding="{Binding Path=Cells[Id].Value}" Value="1">
<Setter TargetName="image" Property="Source" Value="blue.png"/>
</DataTrigger>
<DataTrigger Binding="{Binding Path=Cells[Id].Value}" Value="2">
<Setter TargetName="image" Property="Source" Value="red.png"/>
<DataTrigger Binding="{Binding Path=Cells[Id].Value}" Value="3">
<Setter TargetName="image" Property="Source" Value="yellow.png"/>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Hope this helps.