从列表视图中检索数据

时间:2015-07-13 05:40:23

标签: c# listview windows-store-apps itemsource

在我的Windows商店应用程序中,有一个列表视图,它使用项目源来获取数据。它看起来像这样:

x = 0

while x <= 5

File.open("yahoo_accounts.txt") do |email|
    email.each do |item|
    email, password = item.chomp.split(',')
    emails << email 
    passwords << password
    emails.zip(passwords) { |name, pass| 
        browser = Watir::Browser.new :ff
        browser.goto "url"

    #logs in and does what its suppose to do with the name and pass

        }
    end
    x += 1
    next
end
end

我的C#代码

<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>

当用户选择列表视图中的项目时,我需要检索其ID。但我不明白该怎么做。谁能告诉我怎么做?

1 个答案:

答案 0 :(得分:2)

您需要向SelectionChanged添加ListView事件并实施。

public void ItemSelected(object sender, SelectionChangedEventArgs args)
{
    var item= lsvLinks.SelectedItem as Collection;
    int ID = item.ID;
}

在ListView上,您可以添加如下事件。

<ListView x:Name="lsvLinks" IsItemClickEnabled="True" SelectionMode="Single" ItemsSource="{Binding Source={StaticResource cvs2}}" SelectionChanged="ItemSelected" >