我正在使用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事件会根据需要触发。