gpt4 book ai didi

c# - 如何在过滤 ICollectionView 后选择第一项

转载 作者:太空宇宙 更新时间:2023-11-03 20:52:56 27 4
gpt4 key购买 nike

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

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

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

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

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;
}
}
}

View 模型:

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”),然后在第 2 列的文本中显示文本“This is number 0”。

我已经尝试了 SelectedIndex=0 和 MoveCurrentToFirst 但没有选择任何东西。

最佳答案

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

myCollectionView.MoveCurrentTo(someItem);
...
myCollectionView.MoveCurrentToFirst();

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

<ListView IsSynchronizedWithCurrentItem="True" ...

检测何时应用过滤器

评估过滤器时,会刷新 Collection View ,从而重置集合。要检测到这一点,请监听 CollectionChanged 事件并查找 NotifyCollectionChangedAction.Reset 标志。请引用CollectionView source code了解更多详情。

关于c# - 如何在过滤 ICollectionView 后选择第一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53431070/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com