IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Windows Presentation Foundation Discussion :

Binding via code, mode twoway


Sujet :

Windows Presentation Foundation

  1. #1
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 537
    Points : 369
    Points
    369
    Par défaut 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 : 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");
                    }
                }
            }
    J'ai un CustomControl.
    Afin de réaliser un binding j'ai donc créer une dependdencyproperty dans mon 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
     
    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 : 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);
    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.

  2. #2
    Membre éprouvé Avatar de jmix90
    Homme Profil pro
    Consultant .Net
    Inscrit en
    Juillet 2007
    Messages
    576
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Consultant .Net
    Secteur : Conseil

    Informations forums :
    Inscription : Juillet 2007
    Messages : 576
    Points : 998
    Points
    998
    Par défaut
    Hello,

    Effectivement, les méthodes get/set des DP sont uniquement la pour aider le développeur(et apparemment elles provoquent aussi beaucoup de confusion ) le moteur de binding ne passe pas par elle pour mettre à jour les valeurs met agit directement au niveau de la DP.

    Si tu veux suivre les changements de valeur tu peux utiliser un callback. Voici un exemple :
    Code C# : 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
     
    #region Count
     
    /// <summary>
    /// Count Dependency Property
    /// </summary>
    public static readonly DependencyProperty CountProperty =
        DependencyProperty.Register("Count", typeof(int), typeof(CustomControl1),
            new FrameworkPropertyMetadata((int)0,
                new PropertyChangedCallback(OnCountChanged)));
     
    /// <summary>
    /// Gets or sets the Count property. This dependency property 
    /// indicates ....
    /// </summary>
    public int Count
    {
        get { return (int)GetValue(CountProperty); }
        set { SetValue(CountProperty, value); }
    }
     
    /// <summary>
    /// Handles changes to the Count property.
    /// </summary>
    private static void OnCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        CustomControl1 target = (CustomControl1)d;
        int oldCount = (int)e.OldValue;
        int newCount = target.Count;
        target.OnCountChanged(oldCount, newCount);
    }
     
    /// <summary>
    /// Provides derived classes an opportunity to handle changes to the Count property.
    /// </summary>
    protected virtual void OnCountChanged(int oldCount, int newCount)
    {
        //Agit sur la prop
    }
     
    #endregion

  3. #3
    Membre averti
    Profil pro
    Inscrit en
    Janvier 2008
    Messages
    537
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2008
    Messages : 537
    Points : 369
    Points
    369
    Par défaut
    Thanks mucho beaucoup.

    Ça m'a bien aidé.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Binding Mode=TwoWay sans effet
    Par Krustig dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 18/06/2009, 14h42
  2. Réponses: 2
    Dernier message: 05/10/2006, 12h01
  3. Réponses: 5
    Dernier message: 03/08/2006, 16h13
  4. [Conception] Question: Gestion Incrementation via SQL ou VIA Code PHP ???
    Par ronio dans le forum PHP & Base de données
    Réponses: 8
    Dernier message: 15/02/2006, 13h59
  5. [CR 9] [ASPX][C#]connexion via code
    Par nannous dans le forum Connectivité
    Réponses: 3
    Dernier message: 20/08/2003, 15h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo