Bonjour à tous,
Je développe sur application Silverlight 4, dans lequel j'implemente le MVVM avec le toolkit de Laurent Bugnion.
Ma problèmatique est la suivante:
J'ai plusieurs ViewModel dont j'ai besion dans une seule est même View (vue). J'ai pu voir que cela était possible en utilisant un pattern Locator.
Malheureusement, tous les exemples que j'ai pu voir sur les Locator utilisent un seul ViewModel et à partir du moment où j'essaie le binding avec plusieurs ViewModel, plus rien ne fonctionne.
Voici un exemple simple de ce que cherche à faire.
Les objets du model (Objet1 et Objet2):
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 namespace SilverlightTestLocator.Model { public class Objet1 : INotifyPropertyChanged { private string name; public string Name { get { return name; } set { name = value; if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } public event PropertyChangedEventHandler PropertyChanged; } }Les ViewModel (ViewModel1 et ViewModel2)
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 namespace SilverlightTestLocator.Model { public class Objet2 : INotifyPropertyChanged { private string name; public string Name { get { return name; } set { name = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } public event PropertyChangedEventHandler PropertyChanged; } }
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 namespace SilverlightTestLocator.ViewModel { public class ViewModel1 : INotifyPropertyChanged { private Objet1 obj1 = new Objet1(); public Objet1 Obj1 { get { if (obj1 == null) { obj1 = new Objet1() { Name = "Mon objet 1" }; } return obj1; } set { obj1 = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Obj1")); } } public event PropertyChangedEventHandler PropertyChanged; } }Mon Locator
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 namespace SilverlightTestLocator.ViewModel { public class ViewModel2 : INotifyPropertyChanged { private Objet2 obj2 = new Objet2(); public Objet2 Obj2 { get { if (obj2 == null) { obj2 = new Objet2() { Name = "Mon objet 2" }; } return obj2; } set { obj2 = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Obj2")); } } public event PropertyChangedEventHandler PropertyChanged; } }
Création de la ressource static Locator dans App.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 namespace SilverlightTestLocator.ViewModel { public class Locator : INotifyPropertyChanged { private ViewModel1 wm1; public ViewModel1 Wm1 { get { return wm1; } set { wm1 = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Wm1")); } } private ViewModel2 wm2; public ViewModel2 Wm2 { get { return wm2; } set { wm2 = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Wm2")); } } public event PropertyChangedEventHandler PropertyChanged; } }
La View dans laquelle je souhaite faire le binding
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9 <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:viewModel="clr-namespace:SilverlightTestLocator.ViewModel" x:Class="SilverlightTestLocator.App" > <Application.Resources> <viewModel:Locator x:Key="LocatorVM"/> </Application.Resources> </Application>
Bien sure le binding ne fonctionne pas
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 <UserControl x:Class="SilverlightTestLocator.MainPage" 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:viewModel="clr-namespace:SilverlightTestLocator.ViewModel" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400" DataContext="{Binding Source={StaticResource LocatorVM} }"> <Grid x:Name="LayoutRoot" Background="White"> <StackPanel> <TextBlock Text="Objet1:" /> <TextBox Text="{Binding Path=Vm1.Obj1.Name}" /> <TextBlock Text="Objet2:" /> <TextBox Text="{Binding Path=Wm2.Obj2.Name}" /> </StackPanel> </Grid> </UserControl>
Merci par avance pour votre aide![]()
Partager