如何在UWP中遍历ListView?

时间:2019-04-05 13:53:13

标签: c# loops listview uwp

我有一个ListView:

<ListView x:Name="lvVorlagen">
  <ListView.ItemContainerStyle>
     <Style TargetType="ListViewItem">
        <Setter Property="MinHeight" Value="1"/>
        <Setter Property="Height" Value="30"/>
     </Style>
  </ListView.ItemContainerStyle>
</ListView>

我在其中添加了一些项目:

lvVorlagen.Items.Add("A");
lvVorlagen.Items.Add("B");
lvVorlagen.Items.Add("C");
lvVorlagen.Items.Add("D");

我如何遍历项目并检查它们是否被检查或以编程方式检查它们?我已经习惯了

foreach (ListViewItem bla in this.lvVorlagen.Items)
{
  if (bla.IsSelected)
  {
    tbMail.Text = "ASD";
  }
}

但是我得到:

  

System.InvalidCastException:“无法转换类型的对象   “ System.String”以键入“ Windows.UI.Xaml.Controls.ListViewItem”。”

下次尝试:

for (int i = 0; i < this.lvVorlagen.Items.Count; i++)
{
  if (this.lvVorlagen.Items[i].IsSelected)
  {
    tbMail.Text = "ASD";
  }
}

但是然后,我在Equals仅拥有ToStringthis.lvVorlagen.Items[i]等。

我读了很多有关装订的文章,但是我不知道如何装订项目。

2 个答案:

答案 0 :(得分:0)

您可以使用ContainerFromItem方法获取对特定项目的ListViewItem容器的引用:

foreach (object item in this.lvVorlagen.Items)
{
    ListViewItem lvi = this.lvVorlagen.ContainerFromItem(item) as ListViewItem;
    if (lvi != null && lvi.IsSelected)
    {
        tbMail.Text = "ASD";
    }
}

答案 1 :(得分:0)

mm8的答案将允许您遍历ListItems。另一种方法是通过ObservableCollection使用数据绑定。这将简化迭代过程,因为您无需遍历列表项,而是遍历绑定到ListView的集合。要进行选择,只需查看MainList.SelectedItem即可得出对象类型(在这种情况下为字符串)。我还添加了一个如何更改值的示例。您可以在ObservableCollection上使用LINQ表达式来实现排序。

XAML

    <Grid Background="Transparent" Margin="15">
    <Grid.RowDefinitions>
        <RowDefinition Height="*" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <ListView x:Name="lvVorlagen"
              Margin="0,0,0,15"
              Foreground="Black"
              ItemsSource="{x:Bind LvVorlagen, Mode=OneWay}"
              HorizontalAlignment="Stretch"
              VerticalAlignment="Stretch">
        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Setter Property="MinHeight" Value="1"/>
                <Setter Property="Height" Value="30"/>
            </Style>
        </ListView.ItemContainerStyle>
        <ListView.ItemTemplate>
            <DataTemplate>
                <TextBlock  Text="{Binding}" />
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    <Button x:Name="btnIterate" 
            Content="Iterate" 
            Grid.Row="1" 
            Height="40" 
            FontSize="18" 
            Background="Orange"
            Foreground="Black"
            HorizontalAlignment="Right" 
            Click="BtnIterate_Click"/>
    <Button x:Name="btnSelect" 
            Content="Select" 
            Grid.Row="1" 
            Height="40" 
            FontSize="18" 
            Background="LightBlue"
            Foreground="Black"
            HorizontalAlignment="Left"
            Click="BtnSelect_Click"/>
    <Button x:Name="btnChange" 
            Content="Change" 
            Grid.Row="1" 
            Height="40" 
            FontSize="18" 
            Background="LightGreen"
            Foreground="Black"
            HorizontalAlignment="Center"
            Click="BtnChange_Click"/> 
</Grid>

隐藏代码

  public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        LvVorlagen = new ObservableCollection<string>();
    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        LvVorlagen.Add("A");
        LvVorlagen.Add("B");
        LvVorlagen.Add("C");
        LvVorlagen.Add("D");


    }

    public ObservableCollection<string> LvVorlagen
    {
        get;
        set;
    }

    private void BtnIterate_Click(object sender, RoutedEventArgs e)
    {
        foreach(string value in LvVorlagen)
        {
            Debug.WriteLine(value);
        }
    }

    private void BtnSelect_Click(object sender, RoutedEventArgs e)
    {
        if (MainList.SelectedItem != null)
        {
            Debug.WriteLine("Select Item is: " + MainList.SelectedItem.ToString());
        }
        else
        {
            Debug.WriteLine("No items are selected.");
        }
    }
    private void BtnChange_Click(object sender, RoutedEventArgs e)
    {
        int selectedIndex = MainList.SelectedIndex;
        LvVorlagen[selectedIndex] = "X";
    }
}