Liste de UserControl liée à une Liste du Model
Bonjour,
je débute avec MVVM et j utilise MVVM Light.
MyView possède 4 instances d un UserControl qui vont chacune afficher un scoreboard (classe avec 2 participants et des points pour le score de chaque).
Le scoreboard lié à chaque UserControl n est pas connu à l avance car il est sélectionné dans un autre VM de l application.
En gros je vais avoir une liste de scoreboard (max 4) que je veux afficher dans ma liste de UserControl.
Pour le moment j ai juste disposé ces 4 UserControl les un après les autres sans conteneur particulier dans ma View et je voudrais les lier à mon Model.
Mais je suis vraiment perdu sur la marche à suivre en respectant MVVM avec MVVM Light.
PS: J ai aussi besoin de ma liste de scoreboard dans d autres VM (par exemple là ou l utilisateur sélectionne les scoreboards qu il souhaite) c est donc une propriété static de mon Model et non pas une propriété du VM en question pour l instant.
Et je veux que quand le Scoreboard change, le changement s applique sur la View qui affiche ce scoreboard.
J espère que c est assez clair hum.
Voilà ce que j ai pour l instant, mais j imagine que je ne suis pas parti dans la bonne direction...
MODEL
(Les propriétés du Model comme "PointsP1" ont des valeurs mises à jour par un socket mais peu importe)
Code:
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
|
public class Scoreboard : INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private string _pointsP1 = "0";
public string PointsP1
{
get
{
return _pointsP1;
}
set
{
if (_pointsP1 == value)
{
return;
}
_pointsP1 = value;
OnPropertyChanged("PointsP1");
}
}
}
public class ExternalMatch : INotifyPropertyChanged
{
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
private Scoreboard _score;
public Scoreboard Score
{
get
{
return _score;
}
set
{
if (_score == value)
{
return;
}
_score = value;
OnPropertyChanged("Score");
}
}
}
public class ListExternalMatchesSelected : ObservableCollection<ExternalMatch>
{
public void addMatch(ExternalMatch aMatch)
{
this.Add(aMatch);
aMatch.DisplayOrder = this.Count - 1;
aMatch.Scoreboard = new Model.Score.Scoreboard();
aMatch.Scoreboard.PropertyChanged +=
new PropertyChangedEventHandler(?? OnScoreboardChanged ??);
}
} |
VIEWMODEL:
Code:
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
|
public class MyVM : ViewModelBase
{
//Comment j arrive ici??
public void OnScoreboardChanged(object sender, PropertyChangedEventArgs e)
{
Scoreboard s = sender as Scoreboard;
...
}
private ObservableCollection<ExternalMatch> _listMatches = null;
public ObservableCollection<ExternalMatch> ListMatches
{
get
{
return _listMatches;
}
set
{
if (_listMatches == value)
{
return;
}
_listMatches = value;
RaisePropertyChanged(() => ListMatches);
}
} |
//MA VUE
Contient 4 UserControl qui chacun afficheront un élément de la propriété ListMatches du VM. Comment je les bind? Je pensais faire un binding dynamique dans le créateur de la View mais y a ptet mieux??