c# - Windows Phone 7 - App.ViewModel 重复项

标签 c# silverlight linq windows-phone-7

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

我在 ItemsCollection 中有重复项。在我的列表框中,我只想显示唯一值。我怎样才能捕获那些进行展示?

我需要在此处显示更多数据..

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

在我的 View 模型中,假设我有水果和蔬菜作为属性。

我可以:

ItemCollection[0].fruit = "苹果" ItemCollection[0].vegetable="胡萝卜"

ItemCollection[1].fruit = "梨" ItemColection[1].vegetable="胡萝卜"

ItemCollection[2].fruit = "苹果" itemCollection[2].vegetable = "青 bean "

如果我只想显示我收藏的水果列表,我该怎么做才能避免重复?

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

更多代码:

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

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

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

我需要在 XAML 端做些什么才能实现不同的工作吗?另外,如果我想显示产品编号,我该如何将其添加到上面的 Select(如果我们可以让它工作)?

           <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 的列表值而不是您的元素。

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

你没有提到你的类名或定义,所以,对于下面的定义,

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());

关于c# - Windows Phone 7 - App.ViewModel 重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4870349/

相关文章:

c# - Expander 结合 GridSplitter

c# - 是否可以在加载内容之前更改 WebBrowser 控件的背景颜色?

c# - 使用 linq 赋值

c# - 在 Entity Framework 中实现 "generic"机制来处理时态数据

c# - 在 C# 和反射中从接口(interface)类型转换为具体类型

c# - 如何解码 JWT Token?

c# - 将 Linq2SQL 简单查询转换为 CompiledQueries 以提高应用程序性能

c# - WriteableBitmap 严重失败,像素阵列非常不准确

c# - Linq OrderBy 字符串

c# - 将多维集合转换为带分隔符的字符串