Binding via code, mode twoway
Bonjour,
J'ai voulu essayer un binding via le code mais j'ai un petit souci.
Propriété de mon ViewModel:
Code:
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");
}
}
} |
J'ai un CustomControl.
Afin de réaliser un binding j'ai donc créer une dependdencyproperty dans mon CustomControl:
Code:
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)); |
Puis je créé mon customcontrol via le code, et j'essaye donc d'ajouter le binding avec:
Code:
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); |
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.
Le binding est il mal réalisé? (surement:?)
Merci.