Bonjour à tous, je me remet tranquillement au DataBinding (en Silverlight 4) en essayant de faire un petit truc tout simple mais je bloque.

Alors j'ai une classe personne qui ressemble à ça :

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
public class Personne :INotifyPropertyChanged
    {
        public static ObservableCollection<Personne> ListPersonnes = new ObservableCollection<Personne>();
 
        private string _Nom;
        public string Nom
        {
            get
            {
                return this._Nom;
            }
            set
            {
                this._Nom = value;
                OnPropertyChanged("Nom");
            }
        }
 
        private string _Prenom;
        public string Prenom
        {
            get
            {
                return this._Prenom;
            }
            set
            {
                this._Prenom = value;
                OnPropertyChanged("Prenom");
            }
        }
 
 
        private DateTime _DateDeNaissance;
        public DateTime DateDeNaissance
        {
            get
            {
                return this._DateDeNaissance;
            }
            set
            {
                this._DateDeNaissance = value;
            }
        }
 
        public Personne(string _LeNom,string _LePrenom,DateTime _LaDate)
        {
            this.Nom = _LeNom;
            this.Prenom = _LePrenom;
            this._DateDeNaissance = _LaDate;
 
            ListPersonnes.Add(this);
        }
        public Personne()
        {
 
        }
 
        public override string ToString()
        {
            return this.Nom + ' ' + this.Prenom;
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propName)
        {
            PropertyChangedEventHandler eh = this.PropertyChanged;
            if (null != eh)
            {
                eh(this, new PropertyChangedEventArgs(propName));
            }
        }
 
    }
Ensuite dans mon interface j'ai juste une ComboBox que remplie à l'aide de ma static ObservableCollection<Personne> ListPersonnes. Puis j'aimerais qu'en fonction de la selection, le détail de ma personne s'affiche dans un formulaire à côté. j'ai donc procédé comme cela :

Main.xaml.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
public partial class MainPage : UserControl
    {
 
        public Personne SelectedPersonne { get; set; }
 
        public MainPage()
        {
            InitializeComponent();
 
            SelectedPersonne = new Personne();
 
            Personne Personne1 = new Personne("Reagan", "Ronald", new DateTime(1911, 02, 06));
            Personne Personne2 = new Personne("Mitterrand", "François", new DateTime(1916, 09, 26));
            Personne Personne3 = new Personne("Marx", "Karl", new DateTime(1818, 05, 05));
            Personne Personne4 = new Personne("Zedong", "Mao", new DateTime(1893, 12, 26));
            Personne Personne5 = new Personne("Guevara", "Ernesto", new DateTime(1928, 06, 14));
 
            Cb_Personnes.ItemsSource = Personne.ListPersonnes;
        }
 
    }
et mon Main.xaml :
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
<Grid x:Name="LayoutRoot" Background="White" Height="300" Width="400" >
        <Grid.RowDefinitions>
            <RowDefinition Height="150"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition Height="30"></RowDefinition>
        </Grid.RowDefinitions>
 
        <Grid x:Name="GridPersonne" Grid.Row="0" DataContext="{Binding SelectedPersonne,Mode=TwoWay}">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50*"></ColumnDefinition>
                <ColumnDefinition Width="50*"></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="35"></RowDefinition>
                <RowDefinition Height="35"></RowDefinition>
                <RowDefinition Height="35"></RowDefinition>
                <RowDefinition Height="35"></RowDefinition>
                <RowDefinition Height="35"></RowDefinition>
                <RowDefinition Height="35"></RowDefinition>
            </Grid.RowDefinitions>
 
            <!--Les labels-->
            <TextBlock Text="Nom : " Grid.Column="0" Grid.Row="0" HorizontalAlignment="Right"></TextBlock>
            <TextBlock Text="Prenom : " Grid.Column="0" Grid.Row="1" HorizontalAlignment="Right"></TextBlock>
            <TextBlock Text="Date de Naissance : " Grid.Column="0" Grid.Row="2" HorizontalAlignment="Right"></TextBlock>
 
            <!--les composants que l'on va Binder-->
            <TextBox Name="Tb_Nom" Grid.Column="1" Grid.Row="0" Text="{Binding Nom,Mode=TwoWay}"></TextBox>
            <TextBox Name="Tb_Prenom" Grid.Column="1" Grid.Row="1" Text="{Binding Prenom,Mode=TwoWay}"></TextBox>
            <sdk:DatePicker Name="Dp_Date" Grid.Column="1" Grid.Row="2" SelectedDate="{Binding DateDeNaissance,Mode=TwoWay}"/>
        </Grid>
        <TextBlock Text="Liste des personnes : " Grid.Row="1"></TextBlock>
        <ComboBox Name="Cb_Personnes" Grid.Row="2" SelectedItem="{Binding SelectedPersonne,Mode=TwoWay}"></ComboBox>
 
    </Grid>
Déja mon Objet SelectedPersonne ne s'update pas lors d'un changement de selection de ma comboBox.
Si je rajoute à la main sur l'evenement SelectionChanged de ma comboBox :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
Cb_Personnes.SelectionChanged += (o, e) =>
            {
                var r = o as ComboBox;
                SelectedPersonne = r.SelectedItem as Personne;                
            };
Et que j'enlève le binding de SelectedItem de ma comboBox, cela n'update toujours pas mon formulaire je suis obliger de rajouter :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
Cb_Personnes.SelectionChanged += (o, e) =>
            {
                var r = o as ComboBox;
                SelectedPersonne = r.SelectedItem as Personne;
                LayoutRoot.DataContext = SelectedPersonne;
            };
Mais je ne trouve pas ça très propre, je crois que j'ai raté quelque chose mais je ne vois pas quoi.

Cordialement MoZo