Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good Evening All,

I am new to wpf and I am in need of some desperate assistance. I can honestly say I've put more hours searching than I've slept lately.

So far I read in an xml file to an observable collection. Then create a CollectionViewSource in xaml referencing this observable collection. Then databind a datagrid to the properties of the collectionviewsource. All this works correctly.

Now I want to add some customization of the display. Say for example I have 3 names in my CVS.
1.GOLD
2.GOLD
3.SILVER

What I want is just to show two items in my grid... GOLD and SILVER. Just 1 time each. Basically the same as saying SELECT DISTINCT in SQL.

So far I am using multibinding/multiconverted to bring in the values... but I am stuck here.
In fact I don't even know if I am going about it the correct way.

Can someone please point me in the right direction? How can I display only 1 occurrence of the names in my list?

Here is my xaml...
HTML
Window.Resources>
    <local:DataBindingClass x:Key="formatter"/>
    <local:MyDataSource x:Key="MyData"/>

    <CollectionViewSource x:Key="ViewSource" Source="{Binding Source={StaticResource ResourceKey=MyData}, Path=TradesClass}"/>
</Window.Resources
> <window.resources>
        <local:databindingclass x:key="formatter" xmlns:x="#unknown" xmlns:local="#unknown" />
        <local:mydatasource x:key="MyData" xmlns:x="#unknown" xmlns:local="#unknown" />
        
        <collectionviewsource x:key="ViewSource" source="{Binding Source={StaticResource ResourceKey=MyData}, Path=TradesClass}" xmlns:x="#unknown" />
    </window.resources>

<pre>
                    <DataGrid 
						x:Name="DataGridBreakdown" 
						Margin="10" 
						MaxHeight="300" 
						VerticalAlignment="Top"
						CanUserAddRows="False"
						AutoGenerateColumns="False"
						HorizontalContentAlignment="Stretch"
                        ItemsSource="{Binding Source={StaticResource ViewSource}}"
                            >


                        
                        <DataGrid.Resources>
                            <local:BreakdownGridValueConverter x:Key="myConverter"/>
                        </DataGrid.Resources>
                        
                        <!--<DataGrid.DataContext>
                            <local:TradesClass/>
                        </DataGrid.DataContext>-->

                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Market">
                                <DataGridTextColumn.Binding>
                                    <MultiBinding Converter="{StaticResource ResourceKey=myConverter}">
                                        <!--<Binding Path="."></Binding>-->
                                        <Binding Path="tMarketName"/>
                                        <Binding Source="{StaticResource ResourceKey=MyData}"/>
                                    </MultiBinding>
                                </DataGridTextColumn.Binding>
                            </DataGridTextColumn>
                                                
                                                

                            <DataGridTextColumn Header="Delta" Binding="{Binding tDeltaLive}"/>
                            <DataGridTextColumn Header="Prev Pos." Binding="{Binding Path=tDeltaLive}" />
                        </DataGrid.Columns>

                    </DataGrid>
Posted
Updated 23-Jul-13 18:57pm
v3
Comments
Varsha Ramnani 24-Jul-13 1:24am    
you can use LINQ to your observable collection as it is enumerble type you will find DISTINCT function on your collection.
Member 9389444 25-Jul-13 8:03am    
thank you Varsha. There is another piece of the puzzle. If I use LINQ on my observableCollection (originally about 2000 rows), and say for the 1st column of my datagrid is a DISTINCT list of names (now only say 100 rows) like you suggested above, when I go to populate my second column, is the source the original 2000 rows, or is it only the DISTICT 100 rows. Does this question make sense?
Varsha Ramnani 26-Jul-13 2:16am    
read this http://social.msdn.microsoft.com/Forums/silverlight/en-US/25abdc19-b4e1-4d5b-a931-c8cbc78dd05e/how-to-get-distinct-element-of-an-observablecollection .

1 solution

Ok I will try to answer your question:

You need to use the filter in CollectionViewSource. For more info refer this MSDN Link.[^]

You can try this StackOverFlow Answer.[^]

Another One.[^]

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900