Retrieving data from a list
My windows store app has a list view that uses an item source to get data. It looks like this:
<ListView x:Name="lsvLinks" IsItemClickEnabled="True"
SelectionMode="Single"
ItemsSource="{Binding Source={StaticResource cvs2}}" ItemClick="lsvLinks_ItemClick" >
<ListView.ItemsPanel>
<ItemsPanelTemplate>
<WrapGrid Orientation="Vertical" HorizontalChildrenAlignment="left"/>
</ItemsPanelTemplate>
</ListView.ItemsPanel>
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Padding" Value="0"/>
<Setter Property="Margin" Value="-7.5"/>
</Style>
</ListView.ItemContainerStyle>
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Width="340" Height="32" Background="#FFBE9CDE" HorizontalAlignment="Left">
<StackPanel Width="255" VerticalAlignment="Center" Margin="15,0,0,0">
<TextBlock Text="{Binding Link}" Foreground="{Binding Color}" FontSize="15" Margin="0,3,0,0" FontWeight="Normal" VerticalAlignment="Center" HorizontalAlignment="Left"/>
</StackPanel>
<StackPanel Width="50" VerticalAlignment="Center" Margin="0,0,0,0">
<Button x:Name="btnRemove" Width="30" Height="30" Margin="20,0,0,0" ToolTipService.ToolTip="Remove" Click="btnRemove_click">
<Button.Template>
<ControlTemplate>
<Image Source="Assets/cancel.png" Width="30" Height="30"/>
</ControlTemplate>
</Button.Template>
</Button>
</StackPanel>
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
My C # code
try {
IEnumerable < linkTable > obsCollection = (IEnumerable < linkTable > ) await webservice.getLinksStudentAsync(1);
linkList = new List < linkTable > (obsCollection);
int count = 1;
foreach(linkTable linkL in linkList) {
if (linkL.status.Equals("yes")) {
links.Add(new Collection {
ID = count, Link = linkL.link, Type = "Accept", Color = "green", BackColor = "#FFA27BC7"
});
} else if (linkL.status.Equals("no")) {
links.Add(new Collection {
ID = count, Link = linkL.link, Type = "Reject", Color = "Red", BackColor = "#FFA27BC7"
});
} else {
links.Add(new Collection {
ID = count, Link = linkL.link, Type = "Pending", Color = "White", BackColor = "#FFA27BC7"
});
}
count++;
}
cvs2.Source = links;
}
When the user selects an item in the list, I need to get its ID. But I don't understand how to do it. Can anyone tell me how to do this?
+3
source to share
1 answer
You need to add an event SelectionChanged
to ListView
and implement it.
public void ItemSelected(object sender, SelectionChangedEventArgs args)
{
var item= lsvLinks.SelectedItem as Collection;
int ID = item.ID;
}
In the ListView, you can add an event as shown below.
<ListView x:Name="lsvLinks" IsItemClickEnabled="True" SelectionMode="Single" ItemsSource="{Binding Source={StaticResource cvs2}}" SelectionChanged="ItemSelected" >
+2
source to share