Neethu pradeep

Neethu pradeep

  • NA
  • 36
  • 10.6k

How to bind a key of linq into xaml listview

Dec 13 2018 2:48 AM

 

I need Regions list on my UI.Here key is the Region. How bind the key on xaml(UI) as listview.
 
MainViewModel 
  1. public ObservableCollection<WorkItem>ItemDetails { getset; }  
  1. public class WorkItem  
  2. {  
  3.     public string country { getset; }  
  4.     public string capital { getset; }  
  5.     public string Region { getset; }  
  6.     public string S_region { getset; }  
  7.     public double? Area { getset; }  
  8.     public int Population { getset; }  
  9. }  
this is the linq query that i wrote to group items by Region Property.
  1. var queryRegion = from s in ItemDetails 
  2.                               group s by s.Region into newGroup  
  3.                               orderby newGroup.Key  
  4.                               select newGroup;  
 MainPage.xaml
 
  1. <Grid>  
  2.         <TextBlock Text="Regions: " Foreground="White" FontSize="20" Margin="10,20,0,0" FontWeight="Bold"/>  
  3.       
  4.         <ListView Background="White" Margin="10,70,0,0" FontSize="60"  
  5.                   FontWeight="Bold"   
  6.                  Foreground="Black" ItemsSource="{Binding queryRegion}"  
  7.                   VerticalAlignment="Stretch"  
  8.                   HorizontalAlignment="Stretch"  
  9.                  IsItemClickEnabled="True"  
  10.                   ItemClick="ListView_ItemClick">  
  11.          
  12.         <ListView.ItemContainerStyle >  
  13.                 <Style TargetType="ListViewItem" >  
  14.                     <Setter Property="FontSize"  
  15.                             Value="20"  />  
  16.                         <Setter Property="BorderBrush" Value="LightGray" />  
  17.                         <Setter Property="BorderThickness" Value="0,0,0,01"/>  
  18.                     <Setter Property="Margin" Value="10,0,0,0"/>  
  19.   
  20.                 </Style>  
  21.             </ListView.ItemContainerStyle>  
  22.             <ListView.ItemTemplate>  
  23.                 <DataTemplate>  
  24.                     <TextBlock Text="{Binding queryRegion.Key}" Foreground="Black"/>  
  25.                 </DataTemplate>  
  26.             </ListView.ItemTemplate>  
  27.   
  28.         </ListView>  
  29.        
  30.     </Grid>  
 

Answers (3)