绑定路径=期望myObservableCollection属性

时间:2014-09-23 09:51:21

标签: c# wpf data-binding properties datagrid

我有一个名为Stockage的类,它继承了INotifyPropertyChanged。此类设置一些字符串,然后创建ObservableCollections的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));
    }
}

OnPropertyChanged方法在

之上调用

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(结果)的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的ObservableCollection,结果。

我的DataGrid ItemsSource="{Binding Path=result}"上的问题result带来了“预期属性”消息,并且不提供任何其他信息.. < /强>

如果我尝试在结果后.投放,则建议Countthis[int]

我知道这个主题对我的其他主题Cannot resolve property 'myObservableCollection' in data context of type ''namespace.class"看起来很多,实际上,它是第一个主题的以下内容。

希望我提供了足够的信息并且不是太模糊。如果遗漏了某些内容,我可以编辑我的代码。

感谢您的帮助。

问候。

1 个答案:

答案 0 :(得分:2)

您的Stockage班级成员result,但 <{1}}属性应该是:

public

这是public static ObservableCollection<Stockage> result = new ObservableCollection<Stockage>(); 变量,不是属性。它应该这样定义:

public static

请注意它如何调用private ObservableCollection<Stockage> result = new ObservableCollection<Stockage>(); public ObservableCollection<Stockage> Result { get { return result ; } set { result = value; OnPropertyChanged("Result"); } } 方法来报告属性更改... 所有属性应该这样做... 就是你的方式实施 OnPropertyChanged界面。