由MVVM对DataGrid进行CompositeCollection绑定 [英] CompositeCollection binding on a DataGrid by MVVM

查看:140
本文介绍了由MVVM对DataGrid进行CompositeCollection绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 CompositeCollection 称为 cmpc ,其中包含一些ObservableCollection。



我将数据从CompositeCollection转换为DataGrid,并因此为DataGridTextColumn,如下所示:

 < DataGrid x:Name =DonneesBrutesIsReadOnly =TrueItemsSource ={Binding Path = .cmpc}Margin =0,65,0,0AutoGenerateColumns =False EnableRowVirtualization =TrueRowDetailsVisibilityMode =VisibleWhenSelected> 
< DataGrid.RowStyle>
< Style TargetType ={x:Type DataGridRow}>
< Style.Triggers>
< DataTrigger Binding ={Binding .Remarque}Value ={x:Null}>
< Setter Property =VisibilityValue =Collapsed>< / Setter>
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGrid.RowStyle>
< DataGrid.Columns>
< DataGridTextColumn x:Name =PrisEnCompteBinding ={Binding Path = .Flag}Header =Pris En Compte>< / DataGridTextColumn>
< DataGridTextColumn x:Name =PMRQWidth =*Binding ={Binding Path = .PMRQTOTM}Header =PMID>< / DataGridTextColumn>
< DataGridTextColumn x:Name =LibellePMRQWidth =*Binding ={Binding Path =.LibelléTOTApres}Header =LibelléPMRQ>< / DataGridTextColumn>
< DataGridTextColumn x:Name =LigneWidth =40Binding ={Binding Path = .Remarque}Header =LigneIsReadOnly =True>< / DataGridTextColumn>
< DataGridTextColumn x:Name =OTMWidth =*Binding ={Binding Path = .TOTMPMRQ}Header =OTM>< / DataGridTextColumn>
< DataGridTextColumn x:Name =TOTMWidth =50Binding ={Binding Path = .SiModifie}Header =TOTM>< / DataGridTextColumn>
< DataGridTextColumn x:Name =LibelleTOTMWidth =*Binding ={Binding Path =.LibelléTOTApres}Header =LibelléOTM>< / DataGridTextColumn>
< DataGridTextColumn x:Name =GAWidth =70Binding ={Binding Path = .Groupe_D_alerte}Header =GA>< / DataGridTextColumn>
< DataGridTextColumn x:Name =DisciplineWidth =*Binding ={Binding Path = .NomTable}Header =Discipline>< / DataGridTextColumn>
< DataGridTextColumn x:Name =DisciplineSubstitueeWidth =120Binding ={Binding Path = .NomChamp}Header =DisciplineSubstituée>< / DataGridTextColumn>
< /DataGrid.Columns>
< / DataGrid>

我精确的我只想看到行.Remarque不是一个NULL值,它完成了通过 DataGrid.RowStyle

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

数据绑定在不同的 DataGridTextColumn 来自相同的CompositeCollection( cmpc ),但是从不同的可观察集合如前所述。



例如,.Flag来来自ObservableCollection ActiviteCollection ,.PMRQTOTM来自 ResultatCollection ,如.LibelléTOTApres和.LibelléTOTAvant,。学科来自 D98DetailsCollection 等。



它工作得很好,我可以获取数据到datagrid,但我有一个问题。 >

数据打印在ObservableCollection之后的ObservableCollection我的意思是数据没有交叉。它首先从 ResultatCollection 中打印数据,并让 DataGridTextColumn 与另一个ObservableCollection绑定,然后另一个ObservableCollection的Datas打印(例如.D98DetailsCollection),并让其他人 DataGridTextColumn 绑定另一个ObservableCollection空等。



我不明白为什么我在ObservableCollection之后得到ObservableCollection,但它不是我的目标。我想得到交叉数据。 .PMRQTOTM是每个ObservableCollection的primaryKey,所以对于相同的.PMRQTOTM,我想在同一行看到D98DetailsCollection,ActiviteCollection,ResultatCollection等的数据。



我希望我的请求足够精确,请不要犹豫,要求我添加一些代码,也许就像我做了我的CompositeCollection,我的ObservableCollections或其他任何东西。



提前谢谢你的帮助。



问候。

解决方案

我总是避免 CompositeCollection 像瘟疫。他们尴尬地使用,因为你已经发现,真的,完全不必要。我发现将我所有不同类型的对象放在一个集合中是好的,因为它们可以是任何顺序的。您可以通过多种不同的方法来实现,在某些情况下,不同的方法会更好,而其他方法更适合其他情况。



我们可以做的一件事是定义一个基类,其中包含我们要在集合中显示的所有数据类型的公共属性。那么我们的集合可以是基类的类型,我们可以在其中添加任何派生类型。第二个类似的选项是定义一个暴露公共属性的接口,在相关类中实现它,然后使该接口的类型收集。



最后一个选择是简单地使用对象类型的集合。现在我们可以按任何顺序添加任何类型。


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.

解决方案

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.

这篇关于由MVVM对DataGrid进行CompositeCollection绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆