简体   繁体   中英

CompositeCollection binding on a DataGrid by MVVM

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 DataGridTextColumn s binded with another ObservableCollection empty, then the Datas from another ObservableCollection are print (.D98DetailsCollection for example) and let the others DataGridTextColumn s 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.

I always avoid CompositeCollection s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM