Bonjour,
Je cherche à avoir une liste qui défile automatiquement à la fin lorsqu'on ajoute une nouvelle ligne.
Auriez-vous une solution ou une piste pour résoudre mon problème ?
J'ai fait un exemple simple dans lequel je voudrais l'intégrer :
Modèle :
Vue Modèle :
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 public class Student { public string Lastname {get; set;} public string Firstname {get; set;} public Student(string lastname, string firstname) { this.Lastname = lastname; this.Firstname = firstname; } } public class StudentsModel: ObservableCollection<Student> { private static object _threadLock = new Object(); private static StudentsModel current = null; public static StudentsModel Current { get { lock (_threadLock) if (current == null) current = new StudentsModel(); return current; } } private StudentsModel() { for (int i = 1; i <= 50; i++) { Student aStudent = new Student("Student " + i.ToString(), "Student " + i.ToString()); Add(aStudent); } } public void AddAStudent(String lastname, string firstname) { Student aNewStudent = new Student(lastname, firstname); Add(aNewStudent); } }
Vue :
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 public class MainViewModel : ViewModelBase { public StudentsModel Students { get; set; } public MainViewModel() { Students = StudentsModel.Current; } private ICommand _AddStudent; public ICommand AddStudent { get { if (_AddStudent == null) { _AddStudent = new DelegateCommand(delegate() { Students.AddAStudent("New Student lastname", "New Student firstname"); }); } return _AddStudent; } }
Merci
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 <Window x:Class="demo.Views.MainView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:c="clr-namespace:demo.Commands"> <Grid> <ListView Grid.Row="2" BorderBrush="White" ItemsSource="{Binding Path=Students}" HorizontalAlignment="Stretch"> <ListView.View> <GridView> <GridViewColumn Header="Lastname" DisplayMemberBinding="{Binding Path=Lastname}" /> <GridViewColumn Header="Firstname" DisplayMemberBinding="{Binding Path=Firstname}" /> </GridView> </ListView.View> </ListView > <Button Content="Add" Command="{Binding AddStudent}" Margin="601.94,36.866,96.567,419.403" /> </Grid> </Window>
Partager