2014-09-23 12 views
0

的」數據上下文解決財產「myObservableCollection」我有一個叫STOCKAGE類繼承INotifyPropertyChanged的。 這個類設置一些字符串,然後創建ObservableCollections的一個ObservableCollection這的ObservableCollection發送值幾個字符串之前設置無法在類型「namespace.class」

這樣的:

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在稱爲視圖模型的其他類設置和繼承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)到DataGrid的的ObservableCollection,所以我將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> 

,當然我之前,我確切我使用的是類STOCKAGE,包含我ObservableCollections的的ObservableCollection,result類定義<Window.DataContext> <local:Stockage/> </Window.DataContext>

的問題,在我的DataGrid的ItemsSource="{Binding Path=result}",結果「不能用在「WpfApplication3.Stockage`(我namespace.MyClassContainingMyCollectionResult)

類型的數據上下文解決財產我想我叫什麼不對,或者我做了一些禁止的事情,我儘量給出儘可能多的關於結果的信息,我稱之爲,怎麼樣,希望我提供了足夠的信息,並且我不太模糊,如果缺少某些東西,我可以編輯我的代碼。

感謝您的幫助,祝賀您讀了這篇文章結束。

問候。

EDIT1 的OnPropertyChanged的眨了眨眼,而且,我已經修改結果的聲明。

回答

0

根據你,你已經設置你的Window.DataContextStockage類的一個實例:

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

那你是從類嘗試將數據綁定到非公共變量,result,你不能做。相反,您必須將該變量設置爲可以綁定到數據的公共屬性。

此外,它似乎沒有實際上調用您的OnPropertyChanged方法,這意味着您尚未正確實現INotifyPropertyChanged接口。

+0

的第一件事,我已經beclared結果是這樣的:'公共靜態的ObservableCollection 結果=新的ObservableCollection ();'只是'公共靜態的ObservableCollection Load' 而且之前,我有'公共事件PropertyChangedEventHandler的PropertyChanged ; 私人無效OnPropertyChanged(字符串屬性) { 如果(的PropertyChanged!= NULL){ 的PropertyChanged(這一點,新PropertyChangedEventArgs(屬性)); } }'返回結果後,我編輯我的代碼,我想我忘了寫它。 – Flo 2014-09-23 08:12:40

+0

好吧,現在我在'ItemsSource =「{Binding Path = result}上有另外一個錯誤''''''''''''我期望'result'結果'我必須做'result.Something'嗎?如果您喜歡這個新問題,我可以創建另一個主題。 – Flo 2014-09-23 08:34:13

+0

*如果您喜歡這個新問題,我可以創建另一個主題* ...或許這樣會更好,因爲現在您已更改代碼並使這兩個答案看起來很奇怪,因此此問題變得非常混亂。請在您的新問題中包含*完整*錯誤的詳細信息。 – Sheridan 2014-09-23 08:38:33

0

您的問題是result是方法Load中的局部變量,而不是您可以綁定到的屬性。