2016-01-18 135 views
0

我有一個ListView與分組。我想根據TextBox中輸入的文字顯示ListView中的項目。有很多關於過濾ListViewCollectionViewSource的教程,但它們是WPF而不是UWP。我在做什麼:UWP - 與分組過濾ListView

ListView

<ListView x:Name="ContactsListView" 
       ItemTemplate="{StaticResource ContactsTemplate}" 
       ItemsSource="{x:Bind ContactsViewSource.View}" 
       SelectionMode="Single" 
       ShowsScrollingPlaceholders="True" > 

       <ListView.GroupStyle> 
       <GroupStyle> 
        <GroupStyle.HeaderTemplate> 
         <DataTemplate x:DataType="data:GroupingItem"> 
          <TextBlock Text="{x:Bind Key}" 
            Foreground="Blue" 
            Style="{ThemeResource TitleTextBlockStyle}"/> 
         </DataTemplate> 
        </GroupStyle.HeaderTemplate> 
       </GroupStyle> 
       </ListView.GroupStyle> 
    </ListView> 

CollectionViewSourcePage.Resources定義:

<CollectionViewSource x:Name="ContactsViewSource" 
         x:Key="src" 
         IsSourceGrouped="True" /> 

我創建了一個textChanged事件處理程序TextBox

private void SearchBox_TextChanged(object sender, TextChangedEventArgs e) { 
    .... 
    //here I filter ListView items and add them to CollectionViewSource 
    ContactsViewSource.Source = filteredValues; 
} 

但是沒有改變。 ListView不刷新。我不知道該怎麼辦。對於UWP,我無法找到解決此問題的任何解決方案。

當在MainPageconstructor中分配數據時,它正在顯示數據。當我沒有在constructor中分配數據,而是在SearchBox_TextChanged中時,ListView中沒有顯示數據。

回答

4

請注意,x:Bind默認行爲是OneTime! 所以它不會跟蹤任何進一步的變化。

添加x:Bind ContactsViewSource.View, Mode=OneWay以確保它跟蹤更改。

而且,我寧願加CollectionViewSource的來源XAML像

<CollectionViewSource x:Name="ContactsViewSource" 
         x:Key="src" 
         Source="{x:Bind FilterdValues, Mode=OneWay}" 
         IsSourceGrouped="True" /> 

並補充說,作爲一個屬性,讓代碼從INotifyPropertyChanged所以繼承您可以更改代碼FilterValues,而不是總在代碼中重新分配ContactsViewSource.Source ...

-1

嘗試使用綁定而不是x:綁定。