在''namespace.class'类型的数据上下文中无法解析属性'myObservableCollection' [英] Cannot resolve property 'myObservableCollection' in data context of type ''namespace.class"

查看:114
本文介绍了在''namespace.class'类型的数据上下文中无法解析属性'myObservableCollection'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Stockage的类继承了INotifyPropertyChanged。
这个类设置一些字符串,然后创建一个ObservableCollection的ObservableCollection。这个ObservableCollection将值发送到之前设置的几个字符串。



像这样:

  public class Stockage:INotifyPropertyChanged 
{
public string Flag {get;组; }
public string GroupeAlerte {get;组; }
public string Pmid {get;组; }
public string LibellePmrq {get;组; }
public string Ligne {get;组; }
public string Otm {get;组; }
public string Totm {get;组; }
public string LibelleTotm {get;组; }
public string Discipline {get;组; }
public string DisciplineSubstituee {get;组; }
public string Remarque {get;组; }

public static ObservableCollection< Stockage> result = new ObservableCollection< Stockage>();

public static ObservableCollection< Stockage> Load(ObservableCollection< Resultat> listResult,
ObservableCollection< D98Equipements> equipementses,
ObservableCollection< D98Details> detailses,
ObservableCollection< D675> d675Ses,
ObservableCollection< Activite> activitees)
{

foreach(var resultat in listResult)
{

result.Add(new Stockage {Flag = activitees.FirstOrDefault(e => resultat。 TOTMPMRQ == e.PMRQTOTMActivite).Flag.ToString(),
Pmid = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Substring(0,8),
LibellePmrq = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).LibelléTOTApres,
GroupeAlerte = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).Groupe_D_alerte,
Ligne = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Subs tring(8,2),
Otm = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Substring(10,8),
Totm = listResult.FirstOrDefault( e => resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Substring(18,2),
LibelleTotm = equipementses.FirstOrDefault(e => resultat.TOTMPMRQ == e.PMRQTOTM).Libellé,
Discipline = detailses.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).Discipline,
DisciplineSubstituee = detailses.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ)。 ,
Remarque = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).Remarque,
});


}
返回结果;
}


**公共事件PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string property)
{
if(PropertyChanged!= null)
{
PropertyChanged(this,new PropertyChangedEventArgs(property));
}
} **

ObservableCollections设置在另一个类中ViewModel和继承INotifyPropertyChanged太:

  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);
ActiviteCollection = new ObservableCollection< Activite>(_ BDDInterneEntities.Activite);


}
public ObservableCollection< Resultat> ResultatCollection {get;组; }
public ObservableCollection< D98Equipements> D98EquipementsCollection {get;组; }
public ObservableCollection< D98Details> D98DetailsCollection {get;组; }
public ObservableCollection< D675> D675Collection {get;组; }
public ObservableCollection< Activite> ActiviteCollection {get;组; }


public event PropertyChangedEventHandler PropertyChanged;

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

}

我要发送ObservableCollection ObservableCollections( result )到DataGrid,所以我将DataGrid绑定到集合,然后我绑定每个DataGridTextColumn与之前的对应的字符串。像这样:

 < DataGrid x:Name =DonneesBrutesIsReadOnly =TrueItemsSource ={Binding Path = result }Margin =10,65,0,0AutoGenerateColumns =FalseEnableRowVirtualization =TrueRowDetailsVisibilityMode =VisibleWhenSelected> 
< DataGrid.Columns>
< DataGridTextColumn x:Name =PrisEnCompteBinding ={Binding Path = Flag}Header =Pris En Compte>< / DataGridTextColumn>
< DataGridTextColumn x:Name =PMRQWidth =*Binding ={Binding Path = Pmid}Header =PMID>< / DataGridTextColumn>
< DataGridTextColumn x:Name =LigneWidth =40Binding ={Binding Path = Ligne}Header =LigneIsReadOnly =True>< / DataGridTextColumn>
< DataGridTextColumn x:Name =LibellePMRQWidth =*Binding ={Binding Path = LibellePmrq}Header =LibelléPMRQ>< / DataGridTextColumn>
< DataGridTextColumn x:Name =OTMWidth =*Binding ={Binding Path = Otm}Header =OTM>< / DataGridTextColumn>
< DataGridTextColumn x:Name =TOTMWidth =50Binding ={Binding Path = Totm}Header =TOTM>< / DataGridTextColumn>
< DataGridTextColumn x:Name =LibelleTOTMWidth =*Binding ={Binding Path = LibelleTotm}Header =LibelléOTM>< / DataGridTextColumn>
< DataGridTextColumn x:Name =GAWidth =70Binding ={Binding Path = GroupeAlerte}Header =GA>< / DataGridTextColumn>
< DataGridTextColumn x:Name =DisciplineWidth =120Binding ={Binding Path = Discipline}Header =Discipline>< / DataGridTextColumn>
< DataGridTextColumn x:Name =DisciplineSubstitueeWidth =120Binding ={Binding Path = DisciplineSubstituee}Header =DisciplineSubstituée>< / DataGridTextColumn>
< DataGridTextColumn x:Name =RemarqueWidth =*Binding ={Binding Path = .Remarque}Header =Remarque>< / DataGridTextColumn>

< /DataGrid.Columns>
< / DataGrid>

我当然定义< Window.DataContext>
< local:Stockage />
< /Window.DataContext>
之前,我精确地使用了类库,该类包含ObservableCollection的ObservableCollections, result



ItemsSource ={Binding Path = result}我的DataGrid,结果'无法解析WpfApplication3.Stockage类型的数据上下文中的属性(我的namespace.MyClassContainingMyCollectionResult)



我想我说错了,或者我做了一些禁止的事情,我试图尽可能多地提供关于结果的信息,我称之为如何,希望我提供足够的信息,我不是太模糊,我可以感谢您的帮助,恭喜您已经阅读完这篇文章。



p>问候。



EDIT1
OnPropertyChanged闪烁,此外,我修改了声明的结果。


解决方案

根据您,您已将 Window.DataContext 设置为库存 class:

 < Window.DataContext> 
< local:Stockage />
< /Window.DataContext>

然后,您尝试将数据绑定到该类的非公开变量结果,你不能做。相反,您必须将该变量设置为可以将数据绑定到的公共属性。



此外,您实际上并不会调用 OnPropertyChanged 方法,这意味着你没有正确地实现 INotifyPropertyChanged 接口。


I have a class called Stockage which inherit of INotifyPropertyChanged. this class set some strings and then create an ObservableCollection of ObservableCollections. This ObservableCollection send values to the several strings set before.

Like this:

public class Stockage:INotifyPropertyChanged
{
    public string Flag { get; set; }
    public string GroupeAlerte { get; set; }
    public string Pmid { get; set; }
    public string LibellePmrq { get; set; }
    public string Ligne { get; set; }
    public string Otm { get; set; }
    public string Totm { get; set; }
    public string LibelleTotm { get; set; }
    public string Discipline { get; set; }
    public string DisciplineSubstituee { get; set; }
    public string Remarque { get; set; }

    public static ObservableCollection<Stockage> result = new ObservableCollection<Stockage>();

    public static ObservableCollection<Stockage> Load(ObservableCollection<Resultat> listResult,
        ObservableCollection<D98Equipements> equipementses, 
        ObservableCollection<D98Details> detailses, 
        ObservableCollection<D675> d675Ses, 
        ObservableCollection<Activite> activitees)
    {

        foreach (var resultat in listResult)
        {

            result.Add(new Stockage{Flag=activitees.FirstOrDefault(e=>resultat.TOTMPMRQ == e.PMRQTOTMActivite).Flag.ToString(), 
                Pmid = listResult.FirstOrDefault(e=>resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Substring(0,8),
                LibellePmrq = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).LibelléTOTApres,
                GroupeAlerte = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).Groupe_D_alerte,
                Ligne = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Substring(8, 2),
                Otm = listResult.FirstOrDefault(e=>resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Substring(10,8),
                Totm = listResult.FirstOrDefault(e=>resultat.TOTMPMRQ == e.TOTMPMRQ).TOTMPMRQ.Substring(18,2),
                LibelleTotm = equipementses.FirstOrDefault(e=>resultat.TOTMPMRQ == e.PMRQTOTM).Libellé,
                Discipline = detailses.FirstOrDefault(e=>resultat.TOTMPMRQ == e.TOTMPMRQ).Discipline,
                DisciplineSubstituee = detailses.FirstOrDefault(e=>resultat.TOTMPMRQ == e.TOTMPMRQ).Discipline,
                Remarque = listResult.FirstOrDefault(e => resultat.TOTMPMRQ == e.TOTMPMRQ).Remarque,
            });


        }
        return result;
    }


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

the ObservableCollections are set in an other class called ViewModel and inherit of INotifyPropertyChanged too:

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);
        ActiviteCollection = new ObservableCollection<Activite>(_BDDInterneEntities.Activite);


    }
    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 ObservableCollection<Activite> ActiviteCollection { get; set; } 


    public event PropertyChangedEventHandler PropertyChanged;

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

    }

I want to send the ObservableCollection of ObservableCollections (result) to a DataGrid, so I bind the DataGrid to the collection and then I bind each DataGridTextColumn with it's corresponding string set before. Like this:

<DataGrid x:Name="DonneesBrutes" IsReadOnly="True" ItemsSource="{Binding Path=result}" Margin="10,65,0,0" AutoGenerateColumns="False" EnableRowVirtualization="True" RowDetailsVisibilityMode="VisibleWhenSelected">
            <DataGrid.Columns>
                <DataGridTextColumn x:Name="PrisEnCompte" Binding="{Binding Path=Flag}" Header="Pris En Compte"></DataGridTextColumn>
                <DataGridTextColumn x:Name="PMRQ" Width="*" Binding="{Binding Path=Pmid}" Header="PMID"></DataGridTextColumn>
                <DataGridTextColumn x:Name="Ligne" Width="40" Binding="{Binding Path=Ligne}" Header="Ligne" IsReadOnly="True"></DataGridTextColumn>
                <DataGridTextColumn x:Name="LibellePMRQ" Width="*" Binding="{Binding Path=LibellePmrq}" Header="Libellé PMRQ"></DataGridTextColumn>
                <DataGridTextColumn x:Name="OTM" Width="*" Binding="{Binding Path=Otm}" Header="OTM"></DataGridTextColumn>
                <DataGridTextColumn x:Name="TOTM" Width="50" Binding="{Binding Path=Totm}" Header="TOTM"></DataGridTextColumn>
                <DataGridTextColumn x:Name="LibelleTOTM" Width="*" Binding="{Binding Path=LibelleTotm}" Header="Libellé OTM"></DataGridTextColumn>
                <DataGridTextColumn x:Name="GA" Width="70" Binding="{Binding Path=GroupeAlerte}" Header="GA"></DataGridTextColumn>
                <DataGridTextColumn x:Name="Discipline" Width="120" Binding="{Binding Path=Discipline}" Header="Discipline"></DataGridTextColumn>
                <DataGridTextColumn x:Name="DisciplineSubstituee" Width="120" Binding="{Binding Path=DisciplineSubstituee}" Header="Discipline Substituée"></DataGridTextColumn>
                <DataGridTextColumn x:Name="Remarque" Width="*" Binding="{Binding Path=.Remarque}" Header="Remarque"></DataGridTextColumn>

            </DataGrid.Columns>
        </DataGrid>

I of course define <Window.DataContext> <local:Stockage/> </Window.DataContext> before, I precise I'm using the class Stockage, the class containing my ObservableCollection of ObservableCollections, result.

The problem, on the ItemsSource="{Binding Path=result}" of my DataGrid, result 'Cannot resolve property in data context of type "WpfApplication3.Stockage` (my namespace.MyClassContainingMyCollectionResult)

I suppose I call something wrong, Or I do something forbidden. I have tried to give as much informations as possible about what is result, where I call it, how. Hope I gave enough informations and I wasn't too vaguely. I can edit my code if something missing.

Thanks for your help, and Congrats for having read this to the end.

Greetings.

EDIT1 The OnPropertyChanged is blinked, moreover, I have modified the declaration of result.

解决方案

According to you, you have set your Window.DataContext to an instance of your Stockage class:

<Window.DataContext>
    <local:Stockage/>
</Window.DataContext>

Then you are trying to data bind to a non public variable from that class, result, which you cannot do. Instead, you must make that variable a public property, which you can data bind to.

Additionally, it doesn't appear that you are actually calling your OnPropertyChanged method, which means that you haven't implemented the INotifyPropertyChanged interface correctly.

这篇关于在''namespace.class'类型的数据上下文中无法解析属性'myObservableCollection'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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