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 の可視性が状態を取得すると、CollectionChanged はもう起動されません。Visibility プロパティを削除し、それを使用せずに作業を確認すると、必要に応じて CollectionChanged イベントが発生します。