【问题标题】:Cannot resolve property 'myObservableCollection' in data context of type ''namespace.class"无法解析“namespace.class”类型的数据上下文中的属性“myObservableCollection”
【发布时间】:2014-11-17 07:26:10
【问题描述】:

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

像这样:

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));
        }
    }**

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; 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));
            }

    }

我想将 ObservableCollections (result) 的 ObservableCollection 发送到 DataGrid,因此我将 DataGrid 绑定到集合,然后将每个 DataGridTextColumn 与之前设置的相应字符串绑定。像这样:

<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>

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

问题,在我的 DataGrid 的 ItemsSource="{Binding Path=result}" 上,导致“无法解析类型为“WpfApplication3.Stockage”(我的命名空间.MyClassContainingMyCollectionResult)的数据上下文中的属性

我想我说错了,或者我做了一些被禁止的事情。我试图提供尽可能多的信息,说明结果是什么,我在哪里称呼它,如何。希望我提供了足够的信息,而且我不是太含糊。如果缺少某些内容,我可以编辑我的代码。

感谢您的帮助,并恭喜您阅读到最后。

您好。

EDIT1 OnPropertyChanged是闪烁的,而且我修改了result的声明。

【问题讨论】:

    标签: c# wpf data-binding datagrid observablecollection


    【解决方案1】:

    根据您的说法,您已将 Window.DataContext 设置为您的 Stockage 类的实例:

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

    然后您尝试将数据绑定到该类中的非公共变量result,这是您无法做到的。相反,您必须将该变量设为公共属性,您可以对其进行数据绑定。

    此外,您似乎并没有真正调用您的OnPropertyChanged 方法,这意味着您没有正确实现INotifyPropertyChanged 接口。

    【讨论】:

    • 首先,我在public static ObservableCollection&lt;Stockage&gt; Load 之前声明了这样的结果:public static ObservableCollection&lt;Stockage&gt; result = new ObservableCollection&lt;Stockage&gt;(); 此外,我有` public event PropertyChangedEventHandler PropertyChanged;私人无效OnPropertyChanged(字符串属性){如果(PropertyChanged!= null){PropertyChanged(this,新PropertyChangedEventArgs(property)); } }`返回结果后,我编辑了我的代码,我想我忘了写它。
    • 好的,现在我在ItemsSource="{Binding Path=result}" 上出现另一个错误result 上的预期属性我必须做result.Something 吗?如果您喜欢这个新问题,我可以创建另一个主题。
    • 如果你喜欢这个新问题,我可以创建另一个主题...也许这样更好,因为现在你已经更改了代码,这个问题变得非常混乱并使这两个答案看起来很奇怪。请在您的新问题中包含完整错误详细信息。
    【解决方案2】:

    您的问题是result 是方法Load 中的局部变量,而不是您可以绑定到的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      相关资源
      最近更新 更多