Windows Phone 7 - App.ViewModel重复

web*_*ad3 1 c# linq silverlight windows-phone-7

App.ViewModel.RefreshItems();
lbFruit.ItemsSource = App.ViewModel.ItemCollection;
Run Code Online (Sandbox Code Playgroud)

我在ItemsCollection中有重复项.在我的列表框中,我只想显示唯一值.我怎么才抓住那些展示?

我需要在这里表现出更多的数据做.

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

在我的视图模型中,我可以说我有水果和蔬菜作为属性.

我本可以有:

ItemCollection [0] .fruit ="Apple"ItemCollection [0] .vegetable ="Carrot"

ItemCollection [1] .fruit ="Pear"ItemColection [1] .vegetable ="Carrot"

ItemCollection [2] .fruit ="Apple"itemCollection [2] .vegetable ="Green Beans"

如果我只想在我的收藏中显示水果列表,我怎么能这样做而不重复?

例如,我的收藏中可以有多个水果和多个蔬菜.如果我只在我的列表中显示水果,我怎么才能显示:Apple,Pear,Orange

更多代码:

当我按照以下建议执行distinct时: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>
Run Code Online (Sandbox Code Playgroud)

希望这一切都有意义...感谢您的帮助!

dec*_*one 6

这个怎么样:

lbFruit.ItemsSource = App.ViewModel.ItemCollection.Select(item => item.fruit).Distinct();
Run Code Online (Sandbox Code Playgroud)

编辑:

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

要解决这个问题,您需要在IEqualityComparer<T>内部使用Distinct().

您没有提到您的类名或定义,因此,对于以下定义,

public class ProductItem
{
    public int ProductNumber { get; set; }
    public String Fruit { get; set; }
    public String Vegetable { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

您需要创建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();
    }
}
Run Code Online (Sandbox Code Playgroud)

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

lbFruit.ItemsSource = App.ViewModel.ItemCollection.Distinct(new ProductItemByFruitComparer());
Run Code Online (Sandbox Code Playgroud)