如何 select 筛选 ICollectionView 后的第一项

How to select first item after filtering ICollectionView

我在我的视图模型中将 ListView 绑定到 ICollectionView。 ICollectionView 有一些预定义的过滤器,当您单击某些按钮时会应用这些过滤器。但是,在过滤集合后,我似乎找不到任何方法来(自动)select ListView 中的第一项。

我尝试设置 SelectedIndex=0,将 Target 和 Source 通知都添加到绑定中,但是在应用过滤器时都无效。

关于如何实现这一点的任何指示?

编辑:下面的代码说明了我要说的问题。

XAML:

<Window x:Class="CollectionViewTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:CollectionViewTest"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Window.DataContext>
        <local:MainViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!-- MENU -->
        <StackPanel Orientation="Vertical">
            <Button Content="Numbers below 4" Click="Below4_Click" Width="100"/>
            <Button Content="Numbers below 7" Click="Below7_Click" Width="100"/>
            <Button Content="All numbers" Click="All_Click" Width="100"/>
        </StackPanel>

        <!-- LIST -->
        <ListView 
            Grid.Column="1" 
            SelectedIndex="0"
            ItemsSource="{Binding Numbers, Mode=OneWay}"
            SelectedItem="{Binding SelectedNumber, Mode=TwoWay}">
            <ListView.Resources>
                <DataTemplate DataType="{x:Type local:Number}">
                    <TextBlock Text="{Binding Value}" />
                </DataTemplate>
            </ListView.Resources>
        </ListView>

        <!-- DETAILS -->
        <TextBlock Grid.Column="2" Text="{Binding SelectedNumber.Text}" Width="100"/>
    </Grid>
</Window>

代码隐藏:

using System.Windows;

namespace CollectionViewTest
{
    public partial class MainWindow : Window
    {
        private MainViewModel vm;
        public MainWindow()
        {
            InitializeComponent();
            vm = (MainViewModel)DataContext;
        }

        private void Below4_Click(object sender, RoutedEventArgs e)
        {
            vm.MenuFilter = f => f.Value < 4;
        }

        private void Below7_Click(object sender, RoutedEventArgs e)
        {
            vm.MenuFilter = f => f.Value < 7;
        }

        private void All_Click(object sender, RoutedEventArgs e)
        {
            vm.MenuFilter = f => true;
        }
    }
}

视图模型:

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Data;
using System.Collections.ObjectModel;

namespace CollectionViewTest
{
    public class MainViewModel : PropertyChangedBase
    {

        public MainViewModel()
        {
            Numbers = new ObservableCollection<Number>();
            NumberCollection = CollectionViewSource.GetDefaultView(Numbers);
            NumberCollection.Filter = Filter;
            NumberCollection.SortDescriptions.Add(new SortDescription("Value", ListSortDirection.Ascending));

            for (int i = 0; i < 10; i++)
                Numbers.Add(new Number { Value = i, Text = $"This is number {i}." });

        }

        private Func<Number, bool> menuFilter;
        public Func<Number, bool> MenuFilter
        {
            get => menuFilter;
            set
            {
                menuFilter = value;
                NumberCollection.Refresh();
            }
        }

        private bool Filter(object item)
        {
            var number = (Number)item;
            return MenuFilter == null ? true : MenuFilter(number);
        }

        public ObservableCollection<Number> Numbers { get; set; }
        public ICollectionView NumberCollection { get; set; }

        private Number selectedNumber;
        public Number SelectedNumber { get => selectedNumber; set => Set(ref selectedNumber, value); }
    }

    public class Number : PropertyChangedBase
    {
        public int Value { get; set; }

        private string text;
        public string Text { get => text; set => Set(ref text, value); }
    }

    public class PropertyChangedBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected void Set<T>(ref T field, T newValue = default(T), [CallerMemberName] string propertyName = null)
        {
            field = newValue;
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

如您所见,按下其中一个按钮会更改过滤器并调用集合的刷新。我想要的是,列表中的第一项(此处为“0”)自动 selected,然后会在第 2 列的文本中显示文本 "This is number 0"。

我已经尝试了 SelectedIndex=0 和 MoveCurrentToFirst,但都没有 selected。

绑定到 ICollectionView 时不要设置 SelectedIndex。相反,通过 MoveCurrentTo() or MoveCurrentToFirst():

设置其 CurrentItem
myCollectionView.MoveCurrentTo(someItem);
...
myCollectionView.MoveCurrentToFirst();

此外,在您的 ListView 上设置 IsSynchronizedWithCurrentItem:

<ListView IsSynchronizedWithCurrentItem="True" ...

检测何时应用过滤器

评估过滤器时,会刷新集合视图,从而重置集合。要检测到这一点,请监听 CollectionChanged 事件并查找 NotifyCollectionChangedAction.Reset 标志。详情请参考CollectionView source code