通过Entity Framework将数据从几个表绑定到DataGridTextColumn

时间:2014-09-15 08:51:05

标签: c# wpf entity-framework data-binding datagrid

在使用Entity Framework创建我的DataBase之后,我创建了4个可观察的集合,其中包含我拥有的4个表,以及一个CompositeCollection来重新组合,如下所示:

class ViewModel:INotifyPropertyChanged
{
private BDDInterneEntities _BDDInterneEntities;

public ViewModel()
{
    _BDDInterneEntities = new BDDInterneEntities();
    ResultatCollection = new ObservableCollection<Resultat>(_BDDInterneEntities.Resultat);
    D98EquipementsCollection = new ObservableCollection<D98Equipements>(_BDDInterneEntities.D98Equipements);
    D98DetailsCollection = new ObservableCollection<D98Details>(_BDDInterneEntities.D98Details);
    D675Collection = new ObservableCollection<D675>(_BDDInterneEntities.D675);
    cmpc = new CompositeCollection();

    CollectionContainer cc1 = new CollectionContainer();
    CollectionContainer cc2 = new CollectionContainer();
    CollectionContainer cc3 = new CollectionContainer();
    CollectionContainer cc4 = new CollectionContainer();
    cc1.Collection = ResultatCollection;
    cc2.Collection = D98EquipementsCollection;
    cc3.Collection = D98DetailsCollection;
    cc4.Collection = D675Collection;
    cmpc.Add(cc1);
    cmpc.Add(cc2);
    cmpc.Add(cc3);
    cmpc.Add(cc4);


}
public ObservableCollection<Resultat> ResultatCollection { get; set; }
public ObservableCollection<D98Equipements> D98EquipementsCollection { get; set; }
public ObservableCollection<D98Details> D98DetailsCollection { get; set; }
public ObservableCollection<D675> D675Collection { get; set; }
public CompositeCollection cmpc {get; set; }

public event PropertyChangedEventHandler PropertyChanged;

private void OnPropertyChanged(string property)
{
    if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

}

} 我想要的是将一个ObservaleCollection的一些值绑定到一个DataGridTextColumn,将另一个ObservableCollection的值绑定到另一个DataDataGridTextColumn等。

这是Datagrid,绑定的目标:

<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=.cmpc}" Margin="10,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="PMRQ" Width="*" Binding="{Binding Path=.PMID}" Header="PMID"></DataGridTextColumn>
                <DataGridTextColumn x:Name="LibellePMRQ" Width="*" Binding="{Binding Path=.LibelléTOTApres}" Header="Libellé PMRQ"></DataGridTextColumn>
                <DataGridTextColumn x:Name="Ligne" Width="*"     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="*" 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="*" 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>

第一个DataGridTextColumn是从D98DetailsCollection绑定的,第二个是从ResultatCollection绑定的,等等。如下条件:属性TOTMPMRQ是每个ObservableCollection的公共列是等于。

例如,对于像123456789这样的TOTMPMRQ,我想在TOTMPMRQ等于123456789的第一个DataGridTextColumn中绑定D98DetailsCollection的PMRQ值,然后在第二个DataGridTextColumn中绑定ResultatCollection的libellePMRQ值,其中TOTMPMRQ仍然等于123456789等。为每个DataGridTextColumn从几个ObservableCollections获取一行值。

我的主要困难是制作这个条件。目前,DataGrid只在ObservableCollection之后打印ObservableCollection,而不是组合结果。

我希望我的解释不会太混乱,如果需要,我总是可以编辑我的帖子,不要犹豫,多问我一点,或者更少......

提前感谢您的帮助。

EDIT1:

我创建了一个类TestCollectionList,它继承了一个名为TestCollection的ObservableCollection,如下所示:

public class TestCollectionList : ObservableCollection<TestCollectionList.TestCollection>
    {
        public TestCollectionList() : base()
        {
            Add( new TestCollection());
        }
        public class TestCollection
        {
            private string pmid;
            private string libellepmrq;
            private string ligne;
            private string otm;
            private string totm;
            private string libelleotm;
            private string ga;
            private string discipline;
            private string disciplinesubstituee;

            public TestCollection(string pmid, string libellepmrq, string ligne, string otm, string totm, string libelleotm, string ga, string discipline, string disciplinesubstituee)
            {
                this.pmid = pmid;
                this.libellepmrq = libellepmrq;
                this.ligne = ligne;
                this.otm = otm;
                this.totm = totm;
                this.libelleotm = libelleotm;
                this.ga = ga;
                this.discipline = discipline;
                this.disciplinesubstituee = disciplinesubstituee;
            }

            public string Pmid
            {
                get { return pmid; }
                set { pmid = value; }
            }
            public string Libellepmrq
            {
                get { return libellepmrq; }
                set { pmid = value; }
            }
            public string Ligne
            {
                get { return ligne; }
                set { pmid = value; }
            }
            public string Otm
            {
                get { return otm; }
                set { pmid = value; }
            }
            public string Totm
            {
                get { return totm; }
                set { pmid = value; }
            }
            public string Libelleotm
            {
                get { return libelleotm; }
                set { pmid = value; }
            }
            public string Ga
            {
                get { return ga; }
                set { pmid = value; }
            }
            public string Discipline
            {
                get { return discipline; }
                set { pmid = value; }
            }
            public string Disciplinesubstituee
            {
                get { return disciplinesubstituee; }
                set { pmid = value; }
            }

        }
    }

我已经在DataGridTextColumn中创建了我想要绑定的每个属性,现在我想用它们的DataBase的不同值的值来设置它们。例如,pmid必须等于ObservableCollection PMID的{​​{1}}属性,D98DetailsCollection必须设置ObservableCollection ga的{​​{1}}属性等等。

我是好的方式,我绝对错了吗?提前谢谢。

0 个答案:

没有答案