0

I have got a CompositeCollection called cmpc which contains some ObservableCollection.

I sand Datas from the CompositeCollection to a DataGrid and as consequence on it's DataGridTextColumn, like this:

<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=.cmpc}" Margin="0,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding .Remarque}" Value="{x:Null}">
                    <Setter Property="Visibility" Value="Collapsed"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="PrisEnCompte" Binding="{Binding Path=.Flag}" Header="Pris En Compte"></DataGridTextColumn>
        <DataGridTextColumn x:Name="PMRQ" Width="*" Binding="{Binding Path=.PMRQTOTM}" Header="PMID"></DataGridTextColumn>
        <DataGridTextColumn x:Name="LibellePMRQ" Width="*" Binding="{Binding Path=.LibelléTOTApres}" Header="Libellé PMRQ"></DataGridTextColumn>
        <DataGridTextColumn x:Name="Ligne" Width="40" Binding="{Binding Path=.Remarque}" Header="Ligne" IsReadOnly="True"></DataGridTextColumn>
        <DataGridTextColumn x:Name="OTM" Width="*" Binding="{Binding Path=.TOTMPMRQ}" Header="OTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="TOTM" Width="50" Binding="{Binding Path=.SiModifie}" Header="TOTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="LibelleTOTM" Width="*" Binding="{Binding Path=.LibelléTOTApres}" Header="Libellé OTM"></DataGridTextColumn>
        <DataGridTextColumn x:Name="GA" Width="70" Binding="{Binding Path=.Groupe_D_alerte}" Header="GA"></DataGridTextColumn>
        <DataGridTextColumn x:Name="Discipline" Width="*" Binding="{Binding Path=.NomTable}" Header="Discipline"></DataGridTextColumn>
        <DataGridTextColumn x:Name="DisciplineSubstituee" Width="120" Binding="{Binding Path=.NomChamp}" Header="Discipline Substituée"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

I precise I only want to see lines where .Remarque isn't a NULL value, it is done by the DataGrid.RowStyle:

<DataGrid.RowStyle>
                <Style TargetType="{x:Type DataGridRow}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding .Remarque}" Value="{x:Null}">
                            <Setter Property="Visibility" Value="Collapsed"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
</DataGrid.RowStyle>

The datas binded on the differents DataGridTextColumn come from the same CompositeCollection (cmpc) but from differents Observable Collection as explained before.

For example, .Flag come from the ObservableCollection ActiviteCollection, .PMRQTOTM come from ResultatCollection, like .LibelléTOTApres and .LibelléTOTAvant, .Discipline come from D98DetailsCollection, etc.

It works well, I can get datas into the datagrid but I have one problem.

Datas are printed "ObservableCollection after ObservableCollection" I mean the datas are not crossed. It print datas from ResultatCollection first and let the DataGridTextColumns binded with another ObservableCollection empty, then the Datas from another ObservableCollection are print (.D98DetailsCollection for example) and let the others DataGridTextColumns binded with another ObservableCollection empty, etc.

I don't understand why I get ObservableCollection after ObservableCollection but it isn't what I aim. I want to get Crossed Datas. .PMRQTOTM are the "primaryKey" of each ObservableCollection, so for a same .PMRQTOTM, I want to see in the same line the Datas from D98DetailsCollection, ActiviteCollection, ResultatCollection, etc.

I hope I have been precise enough on my request, Don't hesitate to ask me adding some code, maybe like how I made my CompositeCollection, my ObservableCollections, or anything else.

Thank you in advance for your help.

Greetings.

2
  • this is usual behavior of composite collection, you may perhaps try sorting. you may perhaps want to see stackoverflow.com/questions/2155395/…
    – pushpraj
    Sep 18, 2014 at 7:56
  • Wow, sounds quite complex. I see IBindingList could make me get what I look for, I'll try to appropriate this property.
    – Flo
    Sep 18, 2014 at 8:11

1 Answer 1

1

I always avoid CompositeCollections like the plague. They're awkward to use as you have found out and really, totally unnecessary. I find that it is far better to put all of my different types of objects into a single collection, as they can be in any order. You can do this in a number of different ways, with different methods being better in some scenarios and others better suited to other scenarios.

One thing that we can do is to define a base class containing common properties for all of our data types that we want to display in the collection. Then our collection can be of the type of that base class and we can add any derived types into it. A second, similar option would be to define an interface that exposes the common properties, implement it in the relevant classes and then make the collection of the type of that interface.

The final option would be to simply use a collection of type object. Now we can add any types into it and in any order.

4
  • So... If I understand what you say, I have to create a class with every properties I need to make a Big ObservableCollection of every tables I want to get (D675, D98Details, Activite, Resultat, etc.) Then once this Class is done create this big ObservableCollection (I didn't know it was possible to send severals tables to an ObservableCollection?) and Then only call the big ObservableCollection to the DataGrid, is it correct?
    – Flo
    Sep 18, 2014 at 8:20
  • Maybe have you got some links with example, codes or tutorials to help me apply your solution to my case?
    – Flo
    Sep 18, 2014 at 8:34
  • Nope, but it's really not difficult. Just search for 'interface', or 'extend class' if you don't know what that is. There's nothing else to explain... we're just talking about adding items to collections here.
    – Sheridan
    Sep 18, 2014 at 8:44
  • Thanks for all. I think I have enough to perform what I aim now.
    – Flo
    Sep 18, 2014 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.