Answered add image on wpf datagrid with c#

  • Saturday, May 12, 2012 4:20 PM
     
      Has Code

    Hi,

    i want to add a new column in a wpf data grid wich contain an image

    HOW can i add a new image column using code behind?

    if (v.validate(x.Text)) {insert image 1 DataGridTextColumn newclm = new DataGridTextColumn(); newclm.Header = "validate"; dgrecep.Columns.Add(newclm); }

    else

    {

    insert image 2

    }


    • Moved by Alexander Sun Monday, May 14, 2012 8:11 AM Move to more appropriate forum (From:Visual C# General)
    •  

All Replies

  • Saturday, May 12, 2012 6:15 PM
     
     
  • Saturday, May 12, 2012 8:46 PM
     
      Has Code

    Hi,  

    i tried this code but it doesn't waht i want realy, this is the problem

    i want that the image1  and 2 showen in the same content when iclick on boutton

     <Grid>
                      
                        
                        <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding}" Height="328" HorizontalAlignment="Left" Margin="24,84,0,0" Name="dgrecep" VerticalAlignment="Top" Width="603" >
                            <DataGrid.Columns>
                               
                                <DataGridTemplateColumn Header="Content" Width="200">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock Name="txt" Text="{Binding Content}"/>
                                                <Image Name="image" Source="valide.jpg" Visibility="Collapsed" Width="50" Height="50"  />
                                            </StackPanel>
                                            <DataTemplate.Triggers>
                                                <DataTrigger Binding="{Binding Path=Content}" Value="1">
                                                    <Setter TargetName="image" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                        
                                    </DataGridTemplateColumn.CellTemplate>   
                                </DataGridTemplateColumn>
    
    
                                
                                <DataGridTemplateColumn Header="Content" Width="200" >
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock Name="txt" Text="{Binding Content}"/>
                                                <Image Name="image" Source="non_valide.png" Visibility="Collapsed" Width="50" Height="50" />
                                            </StackPanel>
                                            <DataTemplate.Triggers>
                                                <DataTrigger Binding="{Binding Path=Content}" Value="0">
                                                    <Setter TargetName="image" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>

     
  • Friday, May 18, 2012 7:44 AM
    Moderator
     
     Answered Has Code

    Hi DevDOTNETns,

    "i want that the image1  and 2 showen in the same content when iclick on boutton"

    Sorry for that I'm not sure I understand what your mean. Just guess, do you want one column content when cell content value is 1 to show valide image, and when content is 0, to show invalide image. Just one content column, right?

    You just need to add two datatrigger into datatemplate trigger like:

    <DataGridTemplateColumn Header="Content" Width="200">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel>
                                                <TextBlock Name="txt" Text="{Binding Content}"/>
                                                <Image Name="image" Source="valide.jpg" Visibility="Collapsed" Width="50" Height="50"  />
                                            </StackPanel>
                                            <DataTemplate.Triggers>
                                                <DataTrigger Binding="{Binding Path=Content}" Value="1">
                                                    <Setter TargetName="image" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
    
      <DataTrigger Binding="{Binding Path=Content}" Value="0">
                                                    <Setter TargetName="image" Property="Visibility" Value="Visible"/>
                                                </DataTrigger>
    
                                            </DataTemplate.Triggers>
                                        </DataTemplate>
                                        
                                    </DataGridTemplateColumn.CellTemplate>   
                                </DataGridTemplateColumn>

    If I misunderstood you, please feel free to let me know.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us

  • Friday, May 18, 2012 9:17 AM
     
     

    thank you for your help :), i already tried that and it works,show valide,invalide image in the same content,

     my problem still in how to show images when i click on boutton,

    i mean  how can i bind the datagridder with code behind

  • Friday, May 18, 2012 9:26 AM
    Moderator
     
     Answered

    Hi DevDOTNETns,

    You can create a attach property to bind with all image's Visibility property, to control the image.

    Please check out the sample:https://skydrive.live.com/?cid=0b578b4d6ded9b93&sc=documents&id=B578B4D6DED9B93%21107

    Hope it helps.

    Have a nice day.


    Annabella Luo[MSFT]
    MSDN Community Support | Feedback to us