我正在使用 MVVM 开发一个 Winrt 应用程序。我有一个绑定到 ObservableCollection 的 ListView。
<ListView x:Name="TestListView" ItemsSource="{Binding TestItems}" Visibility="{Binding ListVis, Converter={StaticResource BoolToVisibilityConverter}}".../>
我的 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 的 Visibility 获得状态 Collapsed 时,不再触发 CollectionChanged。当我删除 Visibility 属性并检查没有它的工作时,CollectionChanged 事件会根据需要触发。