如果未在ViewModel构造函数中填充ObservableCollection,则ListView不显示数据

时间:2014-07-08 23:11:42

标签: c# wpf mvvm

如果未在viewmodel构造函数中加载ObservableCollection源,则Listview无法显示过滤器数据。

如果我在构造函数中加载ObservableCollection源,然后过滤数据,那么一切正常

我在这里缺少什么。

XAML文件

     <ListView  ItemsSource="{Binding   SourceList}">
 <ListView.ItemTemplate>
  <DataTemplate>
    <StackPanel>>
      <TextBox Text="{Binding foo.OrderID}"> 
   </StackPanel>
  </DataTemplate>
 </ListView.ItemTemplate>
</ListView>
     <TextBox Text="{Binding MyFilter, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>

代码

public abstract class ViewModelBase : INotifyPropertyChanged
{
        public event PropertyChangedEventHandler PropertyChanged;  
        ....  
}   
public class  MyViewModel : ViewModelBase
    {
       public ICollectionView collectionView { get; set; }

        public MyViewModel ()
        {
            repository = new Repository();
             //Fill data
             LoadData();
        }

        public void LoadData()
        {
            using (var context = new Context())
            {
                 var query = (from o in Order
                             join p in Products on o.ProductID equals p.ProductID
                             select o);

           listSourceData = new ObservableCollection<Foo>(query );
           collectionView = CollectionViewSource.GetDefaultView(listSourceData)
                            as ICollectionView;
          }
        }

    private ObservableCollection<Foo> listSourceData;
    public ObservableCollection<Foo> SourceList
    {
        get
        {
            return listSourceData;
        }
        set
        {
            listSourceData = value;
            RaisePropertyChanged();
        }
    }

    private string _filterID;
    public string MyFilter
    {
        get { return _filterID; }
        set
        {
            _filterID= value;
           // Approcah 1 which is working
            FilterCollection();

           // Approcah 2 which is not working
            FilterCollectionWithSearchCriteria();
        }
            RaisePropertyChanged();
        }
    }
    private void FilterCollection()
    {
         // apply filter which works ok as collectionview load in constructor
         collectionView.Filter =...
    }  


    private void FilterCollectionWithSearchCriteria()
    {
        //loadData method in constructor is commented
         loadDataWithID(_filterID);
        // in code I can see its filtered and showing object of Foo
        collectionView.Filter =...
    } 
    public void loadDataWithID(string _filterID)
    {
            using (var context = new Context())
            {
                 var query = (from o in Order
                             join p in Products on o.ProductID equals p.ProductID
                             where o.OrderId==_filterID
                             select o);

           listSourceData = new ObservableCollection<Foo>(query );
           collectionView = CollectionViewSource.GetDefaultView(listSourceData)
                            as ICollectionView;
          }
     } 
  }

2 个答案:

答案 0 :(得分:0)

你应该设置公共属性(SourceList)的值不是(listSourceData),以便RaisePropertyChanged()工作

答案 1 :(得分:0)

不要在构造函数之外创建ObservableCollection的新实例,如下所示:

listSourceData = new ObservableCollection<Foo>(query);

如果您想首先清除集合中的所有项目,请执行以下操作:

public void loadDataWithID(string _filterID)
{
    using (var context = new Context())
    {
        var query = (from o in Order
                     join p in Products on o.ProductID equals p.ProductID
                     where o.OrderId==_filterID
                     select o);

        SourceList.Clear();
        SourceList.AddRange(query.ToList());

    ...
 }