Bonjour,
j'essaie d'ajouter des items un par un dans une listview suivant un filtre. J'utilise la méthode PassesFilter() pour savoir si l'élément considéré peut être ajouté. Le problème est que le premier item est TOUJOURS inséré dans la liste la première fois.
Voici un exemple dans lequel j'insère des chaines de caractères que 'One' à 'Five', en éliminant les chaines de moins de 4 caractères.
Pourquoi est-ce que 'One' se retrouve dans la liste ?
------ window1.xaml -------
------ window1.xaml.cs -------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12 <Window x:Class="TestListviewFilter.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestListviewFilter" Title="Window1" Height="300" Width="300"> <StackPanel> <Button Click="Button_Click">Add to List</Button> <local:FilteredListView x:Name="TheList" /> </StackPanel> </Window>
-------------- FilteredListView.cs -------------------
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace TestListviewFilter { /// <summary> /// Logique d'interaction pour Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { System.Collections.Generic.List<String> oObjects = new List<string>(); oObjects.Add("One"); oObjects.Add("Two"); oObjects.Add("Three"); oObjects.Add("Four"); oObjects.Add("Five"); foreach (object obj in oObjects) { if (TheList.Items.PassesFilter(obj)) TheList.Items.Add(obj); } } } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 using System; using System.Collections.Generic; using System.Text; using System.Windows.Controls; namespace TestListviewFilter { public class FilteredListView : ListView { public FilteredListView() :base() { if (Items.CanFilter) { Items.Filter += new Predicate<object>(ApplyFilter); } } private bool ApplyFilter(object obj) { return (obj.ToString().Length > 4); } } }
Partager