How would i assign distinct attribute value in a ComboBox datacontext?

dynamic-linq linq linq-to-sql wpf wpf-controls

Question

I would like to show all distinct Category in a ComboBox named categoryList

<DataTemplate x:Key="CategoryTemplate">
        <StackPanel Orientation="Horizontal">
            <TextBlock Text="{Binding Path=Category}"/>
        </StackPanel>
    </DataTemplate>
...
 <ComboBox Name="catagoryList" VerticalAlignment="Top" SelectionChanged="categoryList_SelectionChanged" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" ItemTemplate="{StaticResource CategoryTemplate}"/>
...

code behind:

this.categoryList.DataContext = ldc.Books.Select(b => b.Category).Distinct();

but, its not working. How can i do this? Or is there any other way to do that? Thank you.

1
0
11/28/2012 2:23:16 PM

Accepted Answer

Add ToList() to Distinct() and assign it to ItemsSource.

this.categoryList.ItemSource = ldc.Books.Select(b => b.Category).Distinct().ToList();
0
11/28/2012 2:27:53 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow