Bonjour,
J'ai voulu essayer un binding via le code mais j'ai un petit souci.
Propriété de mon ViewModel:
J'ai un CustomControl.
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 public int Count { get { return _Count; } set { if (CountChange != null) { CountChange(value - _Count); } if (_Count != value) { _Count = value; OnPropertyChanged("Count"); } } }
Afin de réaliser un binding j'ai donc créer une dependdencyproperty dans mon CustomControl:
Puis je créé mon customcontrol via le code, et j'essaye donc d'ajouter le binding avec:
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 public int Count { get { return (int)this.GetValue(CountProperty); } set { if (value != _Count) { _Count = value; this.SetValue(CountProperty, value); OnPropertyChanged("Count"); CountString = _Count.ToString(); } } } public static readonly DependencyProperty CountProperty = DependencyProperty.Register("Count", typeof(int), typeof(ButtonCount), new PropertyMetadata(_Count));
Lorsque la propriété "Count" de mon ButtonCount est modifiée la propriété de "Count" de mon ViewModel ce met bien à jour, mais lorsque la propriété de mon ViewModel est elle modifiée, rien ne passe dans le set de celle de mon ButtonCount.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11 ButtonCount aButton = new ButtonCount(); aButton.Style = (Style)FindResource("BtnCount"); ... Binding bCount = new Binding(); bCount.Path = new PropertyPath("Count"); bCount.Source = MyCountViewModel; bCount.Mode = BindingMode.TwoWay; aButton.SetBinding(ButtonCount.CountProperty, bCount);
Le binding est il mal réalisé? (surement)
Merci.
Partager