Omar Harras

Omar Harras

  • NA
  • 2
  • 5.2k

Add a button for each row in a listview

May 3 2016 10:18 AM

I have a listview populated with data from database and a iwould like to add a button in the last column for each row displayed in the listview here is my XAML :

  1. <ListView x:Name="listView" ItemsSource="{Binding}" HorizontalAlignment="Left" Height="520" Margin="27,57,0,0" VerticalAlignment="Top" Width="1133">  
  2.             <ListView.View>  
  3.                 <GridView x:Name="gridView">  
  4.                     <GridViewColumn>  
  5.                         <GridViewColumn.CellTemplate>  
  6.                             <DataTemplate>  
  7.                                 <CheckBox Tag="{Binding fournisseur}" IsChecked="True"/>  
  8.                             </DataTemplate>  
  9.                         </GridViewColumn.CellTemplate>  
  10.                     </GridViewColumn>  
  11.                     <GridViewColumn DisplayMemberBinding="{Binding fournisseur}" Width="280">  
  12.                         <GridViewColumn.Header>  
  13.                             <GridViewColumnHeader Tag="Fournisseur">Fournisseur</GridViewColumnHeader>  
  14.                         </GridViewColumn.Header>  
  15.                     </GridViewColumn>  
  16.                     <GridViewColumn DisplayMemberBinding="{Binding email}" Width="280">  
  17.                         <GridViewColumn.Header>  
  18.                             <GridViewColumnHeader Tag="Email">Email</GridViewColumnHeader>  
  19.                         </GridViewColumn.Header>  
  20.                     </GridViewColumn>  
  21.                     <GridViewColumn Header ="Date" DisplayMemberBinding="{Binding date}" Width="150"/>  
  22.                     <GridViewColumn Header="Fichier CSV" DisplayMemberBinding="{Binding files}" Width="370">  
  23.                         <GridViewColumn.CellTemplate>  
  24.                             <DataTemplate>  
  25.                                 <Button Tag="{Binding}" Content="Open File" Click="OpenFile_OnClick"/>  
  26.                             </DataTemplate>  
  27.                         </GridViewColumn.CellTemplate>  
  28.                     </GridViewColumn>  
  29.                 </GridView>  
  30.             </ListView.View>  
  31.         </ListView>  
and here is my c# code where i fill my listview :  
  1. TOPSAGEEntities db = new TOPSAGEEntities();  
  2.             var query = from fournisseur in db.F_COMPTET  
  3.                         join email in db.F_ECHEANCES on fournisseur.CT_Num equals email.CT_Num  
  4.                         where EntityFunctions.TruncateTime(email.cbModification) == EntityFunctions.TruncateTime(DatePicker.SelectedDate)  
  5.                         select new { fournisseur = (string)email.ECH_Intitule, email = (string)fournisseur.CT_EMail, date = (DateTime)email.cbModification};  
  6.   
  7.             //List Data from Database into the ListView  
  8.             listView.DataContext = query.ToList().Distinct();  
what's wrong with my code ? 

Answers (1)