2017-01-21 21 views
0

ObservableCollectionリストはビューを更新しません。それは正常に見える私にとってObservableCollectionはリストビューを更新しません

DataContext="{Binding Source={StaticResource Locator}, Path=Main}"><FlowDocumentReader BorderBrush = "Black" BorderThickness="2"> 
<FlowDocument> 
    <BlockUIContainer> 
     <ListView BorderThickness = "2" ItemsSource="{Binding Products}"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header = "Lp." DisplayMemberBinding="{Binding Path=OrdinalNumber}" Width="100"/> 
        <GridViewColumn Header = "id" DisplayMemberBinding="{Binding Path=id}" Width="100"/> 
        <GridViewColumn Header = "Name" DisplayMemberBinding="{Binding Path=Name}" Width="100"/> 
        <GridViewColumn Header = "Quantity" DisplayMemberBinding="{Binding Path=Quantity}" Width="100"/> 
        <GridViewColumn Header = "NetPrice" DisplayMemberBinding="{Binding Path=NetPrice}" Width="100"/> 
        <GridViewColumn Header = "GrossPrice" DisplayMemberBinding="{Binding Path=GrossPrice}" Width="100"/> 
        <GridViewColumn Header = "TotalCost" DisplayMemberBinding="{Binding Path=TotalCost}" Width="100"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 
    </BlockUIContainer> 
</FlowDocument> 

:私はこれは私のVM

public class MainViewModel : ViewModelBase{ 
public ObservableCollection<ProductModel> Products { get; set; } 

private void GetData() 
{ 
    //getting data here 

    Products = new ObservableCollection<ProductModel>(); 

    foreach (var item in myData) 
    { 
     Products.Add(item); 
    } 
}} 

XAMLでMVVMライト

を使用しています。問題がどこにあるのか分かりません

答えて

1

これはObservableCollectionでは問題ありませんが、Productsプロパティを設定しています。

Products = new ObservableCollection<ProductModel>(); 

あなたはGetDataメソッドでそれを設定していますが、それについての見解を通知したことがないので、それはあなたのObservableCollectionにバインドされていません。 代わりに、コンストラクタでプロパティを設定することも、プロパティでINotifyPropertyChangedを使用することもできます。

+0

ufffは、私が今参照してください。私はこのプロジェクトを終日しています。私は新鮮な空気が必要です – Quiet

0

従って私はあなたがこれを実行する必要はありません、あなたはすでにViewModelBaseから継承していることを確認:

public event PropertyChangedEventHandler PropertyChanged; 

    // This method is called by the Set accessor of each property. 
    // The CallerMemberName attribute that is applied to the optional propertyName 
    // parameter causes the property name of the caller to be substituted as an argument. 
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
     { 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

は代わりに、あなただけのこの操作を行うことができます。

public class MainViewModel : ViewModelBase{ 
public ObservableCollection<ProductModel> Products { get; set; } 

private void GetData() 
{ 
    //getting data here 

    Products = new ObservableCollection<ProductModel>(); 

    foreach (var item in myData) 
    { 
     Products.Add(item); 
    } 
    RaisePropertyChanged(nameof(Products)); // you are missing this 
}} 
関連する問題