bonjour
J'ai un soucis avec la mis à jour de contrôles Textbox suite à un appuie sur un élément d'une listeBox.
J'ai donc créer un test listeBox mais lorsque je clique sur un élément de ma listbox, je n'arrive pas à mettre à jour les textbox qui se trouve dans la seconde partie de ma fenêtre.
(pour info: je connais le C++, le VB, ... et j'essaie de me former au C# et à l'architecture MVVM avec Visual Studio (dur dur )
ci dessous mes codes:
fichier MainWindow.xaml
Code XAML : 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79 <Window x:Class="TestListBoxWPF.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:TestListBoxWPF" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.DataContext> <local:FicheClientsViewModel></local:FicheClientsViewModel> </Window.DataContext> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <!-- Définition de la Listbox dans la 1ere colonne--> <ListBox x:Name="listeDeFichesClients" SelectionMode="Single" SelectedItem="{Binding SelectedItem, Mode=TwoWay}" Grid.Column="0" BorderBrush="Black" BorderThickness="2" ItemsSource = "{Binding Fiches}" > <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal" > <Label Content="- "></Label> <Label Content="{Binding Nom}"></Label> <Label Content="{Binding Prenom}"></Label> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> <Grid Grid.Column="1"> <Grid.Resources> <Style TargetType="Label"> <Setter Property="HorizontalAlignment" Value="Left"> </Setter> <Setter Property="VerticalAlignment" Value="Center"> </Setter> </Style> <Style TargetType="TextBox"> <Setter Property="HorizontalAlignment" Value="Stretch"> </Setter> <Setter Property="VerticalAlignment" Value="Center"> </Setter> <Setter Property="TextAlignment" Value="Center"> </Setter> </Style> </Grid.Resources> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <!-- Définition des Labels de la 2eme colonne --> <Label Grid.Column="0" Grid.Row="0">Nom:</Label> <Label Grid.Column="0" Grid.Row="1">Prénom:</Label> <Label Grid.Column="0" Grid.Row="2">Genre:</Label> <Label Grid.Column="0" Grid.Row="3">Age:</Label> <Label Grid.Column="0" Grid.Row="4">Profession:</Label> <!-- Définition des TextBox de la 2eme colonne --> <TextBox Name="UserName" Grid.Row="0" Grid.Column="1" Text="{Binding FicheSelectionnee.Nom, Mode=TwoWay}"></TextBox> <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding FicheSelectionnee.Prenom, Mode=TwoWay}"></TextBox> <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding FicheSelectionnee.Age, Mode=TwoWay}"></TextBox> <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding FicheSelectionnee.Genre, Mode=TwoWay}"></TextBox> <TextBox Grid.Row="4" Grid.Column="1" Text="{Binding FicheSelectionnee.Profession, Mode=TwoWay}"></TextBox> </Grid> </Grid> </Window>
fichier FicheClientViewModel.cs
Fichier Utilisateur.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89 using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; using System.Windows; namespace TestListBoxWPF { class FicheClientsViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; public void NotifyPropertyChanged([CallerMemberName] string str = "") { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(str)); } } private ObservableCollection<Utilisateur> fiches; public ObservableCollection<Utilisateur> Fiches { get => fiches; set { if (value != fiches) { fiches = value; NotifyPropertyChanged(); } } } private Utilisateur ficheSelectionnee; public Utilisateur FicheSelectionnee { get => ficheSelectionnee; set { if (value != ficheSelectionnee) { ficheSelectionnee = value; NotifyPropertyChanged(); } } } public FicheClientsViewModel() { Fiches = new ObservableCollection<Utilisateur>(); FicheSelectionnee = new Utilisateur() { Nom = "Lepuis", Prenom = "Pierre", Age = 32, Genre = "Masculin", Profession = "Acteur", }; fiches.Add(FicheSelectionnee); FicheSelectionnee = new Utilisateur() { Nom = "Golum", Prenom = "Isabelle", Age = 67, Genre = "Féminin", Profession = "Serveuse", }; fiches.Add(FicheSelectionnee); } private Utilisateur selectedItem; public Utilisateur SelectedItem { get { return selectedItem; } set { selectedItem = value; NotifyPropertyChanged(); // implementation for all the other list items... } } } }
Si quelqu'un pouvait m'aider
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76 using System.ComponentModel; namespace TestListBoxWPF { internal class Utilisateur : INotifyPropertyChanged { private string nom; private string prenom; private int age; private string genre; private string profession; public event PropertyChangedEventHandler PropertyChanged; public string Nom { get => nom; set { if (value != nom) { nom = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Nom")); } } } public string Prenom { get => prenom; set { if (value != prenom) { prenom = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Prenom")); } } } public int Age { get => age; set { if (value != age) { age = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Age")); } } } public string Genre { get => genre; set { if (value != genre) { genre = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Genre")); } } } public string Profession { get => profession; set { if (value != profession) { profession = value; PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Profession")); } } } } }
En pièce joint j'ai mis la solution visual studio que j'ai créée
Cordialement
Laurent
Partager