App.ViewModel.RefreshItems();
lbFruit.ItemsSource = App.ViewModel.ItemCollection;
I have duplicates in the ItemsCollection. In my listbox I would only like to show unique values. How would I just grab those for display?
I need to show more data here...
In my collection I have 1 set of data that could contain duplicates of certain properties in the collection..
In my view model lets say I have fruits and vegetables as properties.
I could have:
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"
If I only want to display the list of fruits in my collection how would I do that without duplication?
For example I could have Multiple Fruits and Multiple Vegetables in my collection. If I'm only displaying fruits in my list how can I only show: Apple, Pear, Orange
More Code:
When I do the distinct as suggested below: lbFruit.ItemsSource = App.ViewModel.ItemCollection.Select(item => item.fruit).Distinct();
I get 2 *'s (the * is my bullet for the list and is found in the TextBlock in the DataTemplate).
So technically the Distinct is working, but the text is not showing up next to the *'s. As you can see there is also a ProductNumber which I did not show in the original example. However, when I remove that I still get the same 2 *'s.
Is there something I need to do on the XAML side to make the distinct work? Also if I wanted to display the product number how would I add that to the above Select (if we can get it to work)?
    开发者_如何学编程       <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>
Hopefully this all makes sense... Thanks for any and all help!
How about this:
lbFruit.ItemsSource = App.ViewModel.ItemCollection.Select(item => item.fruit).Distinct();
Edit:
Above code does not work in your case because it returns a list of String values and not your items.
To fix that, you need to use IEqualityComparer<T> within Distinct().
You have not mentioned your class name or definition so, for following definition,
public class ProductItem
{
    public int ProductNumber { get; set; }
    public String Fruit { get; set; }
    public String Vegetable { get; set; }
}
You need to create an IEqualityComparer<ProductItem> like following:
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();
    }
}
And then, the following statement should do the trick:
lbFruit.ItemsSource = App.ViewModel.ItemCollection.Distinct(new ProductItemByFruitComparer());
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论