Charanoglu
Charanoglu

Reputation: 1339

How to load different image based on DataGrid cell value?

Suppose I have a DataGrid with a cell that contains a default image:

<Datagrid AutoGenerateColumns="False"                                  ItemsSource="{Binding}">
<DataGrid.Columns>
 <DataGridTextColumn Header="Bookmaker" Binding="{Binding book_name}"/>
   <DataGridTemplateColumn CanUserResize="False">
     <DataGridTemplateColumn.CellTemplate>
       <DataTemplate>
         <Image Source="/App;component/Resources/Assets/foo.png" Width="40" Height="40"
         Stretch="UniformToFill" StretchDirection="Both"
         RenderOptions.BitmapScalingMode="HighQuality" />
         <DataTemplate.Triggers>
         <DataTrigger Binding="{Binding book_name}" Value="test">
         <Setter Property="sour" /> ????
     </DataTrigger>
     </DataTemplate.Triggers>
   </DataTemplate>
 </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I want display as default image foo.png but if the value of the cell contains test I need to change the source of the image, but I cannot find any Source property because I used a DataTemplate, someone has encountered a similar situation?

Upvotes: 1

Views: 861

Answers (1)

Dipen Shah
Dipen Shah

Reputation: 26075

Set style Image and use trigger within that style to choose Source for the Image:

<DataTemplate>
    <Image Width="40" Height="40"
           Stretch="UniformToFill" StretchDirection="Both"
           RenderOptions.BitmapScalingMode="HighQuality">
        <Image.Style>
            <Style TargetType="Image">
                <Setter Property="Source" Value="pack://application:,,,/Assets/foo.png" />
                <Style.Triggers>
                    <DataTrigger Binding="{Binding book_name}" Value="test">
                        <Setter Property="Source" Value="pack://application:,,,/Assets/bar.png" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
    </Image>
</DataTemplate>

Upvotes: 2

Related Questions