UWP - 使用分组过滤ListView

mis*_*hut 1 listview collectionviewsource windows-10 uwp

我有一个ListView分组.我想ListView根据输入的文字显示此项目TextBox.有许多关于过滤ListView和的教程CollectionViewSource,但它们WPF不是UWP.我在做什么:

我的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>
Run Code Online (Sandbox Code Playgroud)

CollectionViewSource的定义是Page.Resources:

 <CollectionViewSource x:Name="ContactsViewSource"
                       x:Key="src"
                       IsSourceGrouped="True" />
Run Code Online (Sandbox Code Playgroud)

我创建了一个textChanged事件处理程序TextBox:

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

但没有变化.ListView未刷新.我不知道该怎么办.我无法找到解决此问题的任何方法UWP.

在其中分配数据时MainPage constructor显示数据.当我没有分配数据constructor但是SearchBox_TextChanged没有显示数据时ListView.

Dep*_*hie 5

请注意x:Bind默认行为是OneTime!所以它不会跟踪任何进一步的变化.

添加x:Bind ContactsViewSource.View, Mode=OneWay以确保它跟踪更改.

另外,我宁愿在XAML中添加CollectionViewSource的Source

 <CollectionViewSource x:Name="ContactsViewSource"
                       x:Key="src"
                       Source="{x:Bind FilterdValues, Mode=OneWay}"
                       IsSourceGrouped="True" />
Run Code Online (Sandbox Code Playgroud)

并将其添加为属性并让代码继承,INotifyPropertyChanged以便您可以在代码中更改FilterValues,而不是始终在代码中重新分配ContactsViewSource.Source ...

  • _"注意 x:Bind 默认行为是`OneTime`"_ - 天哪!谢谢 我最近搬到 UWP 后没有意识到这一点。谢谢 (2认同)