【问题标题】:Check if item exists in ListView检查列表视图中是否存在项目
【发布时间】:2019-11-17 23:08:29
【问题描述】:

我有这个代码:

    <Page.Resources>
            <DataTemplate x:Key="IconTextDataTemplate">
                <StackPanel Orientation="Horizontal" Width="220" Height="60" Background="#FF729FD4">
                    <Border Background="#66727272" Width="40" Height="40" Margin="10">
                        <Image Source="/SampleImage.png" Height="32" Width="32" Stretch="UniformToFill"/>
                    </Border>
                    <StackPanel Orientation="Vertical" VerticalAlignment="Center">
                        <TextBlock Text="{Binding Name}" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
                        <TextBlock Text="{Binding Description}" Margin="10,0,0,0" Width="170" Height="20" TextTrimming="WordEllipsis"/>
                    </StackPanel>
                </StackPanel>
            </DataTemplate>
</Page.Resources>
    <ListView x:Name="Name" ItemTemplate="{StaticResource IconTextDataTemplate}"   Grid.Row="6" Margin="40,20,40,10" HorizontalAlignment="Stretch" Foreground="White" SelectionChanged="DoSomething">
                        <ListView.ItemsPanel>
                            <ItemsPanelTemplate>
                                <ItemsWrapGrid MaximumRowsOrColumns="4"/>
                            </ItemsPanelTemplate>
                        </ListView.ItemsPanel>
                    </ListView>

另一个ListView 与另一个x:Name 具有相同的属性。我需要将项目从一个ListView 复制到另一个。

我有一个代码,我的问题是如何检查一个ListView 中的项目是否已经复制到第二个ListView? 谢谢。

【问题讨论】:

    标签: c# xaml listview


    【解决方案1】:

    如果您只想添加名称,您可以像这样使用 linq

    if(!listView2.Items.Any(item => item.Name == theNameToCheck))
    {
         //copy item
    }
    

    Contains() 将要求您有 IEquatableListViews 项目的类实现:

    class Test : IEquatable<Test>
    {
        public string Name { get; set; }
        public int OtherProperty { get; set; }
    
        public bool Equals(Test other)
        {
            return other.Name == this.Name;
        }
    }
    

    那么你可以这样做:

    if(!listView2.Items.Contains(theItem))
    {
         listView2.Items.Add(theItem);
    }
    

    除非你真的拥有同一个类的实例而不是类的副本(另一个具有相同属性的对象)

    【讨论】:

    • @Tal 有问题吗?
    • listview.Items.Contains(SearchItem); 在 wpf 中不起作用。 IS 包含方法不适用于 wpf?请提供等效代码以在 WPF 中搜索重复
    【解决方案2】:

    假设你只是给列表视图一个简单的列表:

    listView1.Items = myItemList;
    

    并且您使用相同的列表将元素复制到第二个列表视图,您可以简单地这样做:

    MyItem itemToCopy = listView1.SelectedItem; //Or where ever your item comes from
    
    if(!listView2.Items.contains(itemToCopy)
    {
        listView2.Items.add(itemToCopy);
    }
    else
    {
        // Item is already in the list
    }
    

    【讨论】:

      【解决方案3】:

      如果我没听错的话,你会得到两个列表。 您从一个复制到另一个,之后您可以使用 foreach 循环访问该新列表。 我认为它应该是这样的:

      bool found = false;
      
      foreach(ListviewItem originalItem in myOriginalList.Items)
      {
          foreach(ListViewItem copiedItem in myNewList.Items)
          {
               //Check for equality between both items
               if(originalItem.Text == copiedItem.Text)
               {
                    found = true;
                    break;
               }
      
          }
          //Check if the entry is found
          if(found == false)
          {
               //TRY TO COPY AGAIN
          }
          //Set the bool back
          found = false;
      }
      

      【讨论】:

        猜你喜欢
        • 2021-09-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-09-02
        • 2017-12-21
        • 1970-01-01
        相关资源
        最近更新 更多