列表显示。检查项目计数并更改可见性

时间:2013-01-12 22:12:25

标签: listview mvvm-light observablecollection winrt-xaml

我正在使用MVVM开发Winrt应用程序。 我有一个ListView绑定到ObservableCollection。

 <ListView x:Name="TestListView"  ItemsSource="{Binding TestItems}" Visibility="{Binding ListVis, Converter={StaticResource BoolToVisibilityConverter}}".../>

My ObservableCollection:

public class SearchResultCollection : ObservableCollection<TestData>, ISupportIncrementalLoading
    {

 public IAsyncOperation<LoadMoreItemsResult> LoadMoreItemsAsync(uint count)
        {
            var res =  AsyncInfo.Run(async c =>
                                              {
                                                 //ask about data from server
                                                  return new LoadMoreItemsResult() {Count = PAGE_SIZE};

                                              });

            return res;
        }

}

我还有带有链接的RelayCommand的TextBox(在事件“TextChanged”上执行):

Search = new RelayCommand<BehaviorEventArgs>((e) =>
            {
                dynamic sender = e.Sender;
                string text = sender.Text;
                _searchText = text;

                if (string.IsNullOrEmpty(text))
                {
                    TestItems= null;                   
                    return;
                }
                ListVis = false; 
                TestItems= new SearchResultCollection(_searchService, text);
                ((ObservableCollection<TestData>)Contacts).CollectionChanged += SearchViewModel_CollectionChanged;


            });

 void SearchViewModel_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (((ObservableCollection<Contact>)sender).Count != 0)
            {
                ListVis = true;
            }

        }

我正在尝试执行类似https://stackoverflow.com/a/5209102/466545的操作,但是当ListView的可见性获得状态时,ColLCsed不会再触发CollectionChanged。当我删除Visibility属性并检查没有它的工作时,CollectionChanged事件会根据需要触发。

0 个答案:

没有答案