繁体   English   中英

Windows Phone 7-App.ViewModel重复项

[英]Windows Phone 7 - App.ViewModel duplicates

App.ViewModel.RefreshItems();
lbFruit.ItemsSource = App.ViewModel.ItemCollection;

我在ItemsCollection中有重复项。 在我的列表框中,我只想显示唯一值。 我该如何抓住这些东西进行展示?

我需要在这里显示更多数据

在我的集合中,我有1组数据,其中可能包含集合中某些属性的重复项。

在我看来,模型可以说我拥有水果和蔬菜作为属性。

我本可以有:

ItemCollection [0] .fruit =“ Apple” ItemCollection [0] .vegetable =“ Carrot”

ItemCollection [1] .fruit =“梨” ItemColection [1] .vegetable =“ Carrot”

ItemCollection [2] .fruit =“ Apple” itemCollection [2] .vegetable =“青豆”

如果我只想显示集合中的水果列表,该如何做而不重复?

例如,我的收藏中可以有多种水果和多种蔬菜。 如果我仅在列表中显示水果,则只能显示:苹果,梨,橙

更多代码:

当我按照以下建议进行区分时:lbFruit.ItemsSource = App.ViewModel.ItemCollection.Select(item => item.fruit).Distinct();

我得到2 *(*是列表的项目符号,可以在DataTemplate的TextBlock中找到)。

因此从技术上讲Distinct可以正常工作,但是文本不会显示在*旁边。 如您所见,还有一个我在原始示例中未显示的ProductNumber。 但是,当我删除它时,我仍然得到相同的2 *。

我需要在XAML方面做一些事情来使工作变得与众不同吗? 另外,如果我想显示产品编号,如何将其添加到上面的“选择”中(如果可以使用的话)?

           <ListBox x:Name="lbFruit" ItemsSource="{Binding ItemCollection}" SelectionChanged="lbFruit_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel x:Name="DataTemplateStackPanel" Orientation="Horizontal">
                            <TextBlock FontFamily="Segoe WP Semibold" FontWeight="Bold" FontSize="30" VerticalAlignment="Top" Margin="20,10">*</TextBlock>
                            <StackPanel>
                                <TextBlock x:Name="ItemText" Text="{Binding Fruit}"  FontSize="{StaticResource PhoneFontSizeLarge}"/>
                                <TextBlock x:Name="ItemNumber" Text="{Binding ProductNumber}"  FontSize="{StaticResource PhoneFontSizeNormal}"/>
                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

希望这一切都有道理...感谢您提供的所有帮助!

这个怎么样:

lbFruit.ItemsSource = App.ViewModel.ItemCollection.Select(item => item.fruit).Distinct();

编辑:

上面的代码在您的情况下不起作用,因为它返回String值列表,而不是您的项目。

要解决此问题,您需要在Distinct()使用IEqualityComparer<T>

您尚未提及您的班级名称或定义,因此对于以下定义,

public class ProductItem
{
    public int ProductNumber { get; set; }
    public String Fruit { get; set; }
    public String Vegetable { get; set; }
}

您需要创建一个IEqualityComparer<ProductItem> ,如下所示:

public class ProductItemByFruitComparer : IEqualityComparer<ProductItem>
{
    public bool Equals(ProductItem x, ProductItem y)
    {
        // Case-insensitive comparison of Fruit property of both objects evaluates equality between them
        return x.Fruit.Equals(y.Fruit, StringComparison.CurrentCultureIgnoreCase);
    }

    public int GetHashCode(ProductItem obj)
    {
        // Since we are using Fruit property to compare two ProductItems, we return Fruit's HashCode in this method
        return obj.Fruit.GetHashCode();
    }
}

然后,以下语句可以解决问题:

lbFruit.ItemsSource = App.ViewModel.ItemCollection.Distinct(new ProductItemByFruitComparer());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM