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

Silverlight Discussion :

[SL 3] The property 'Command' does not exist on the type 'Button'


Sujet :

Silverlight

  1. #1
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2003
    Messages
    197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Juillet 2003
    Messages : 197
    Points : 115
    Points
    115
    Par défaut [SL 3] The property 'Command' does not exist on the type 'Button'
    Bonjour à tous.

    Pour les besoins d'un projet je fais une petite interface Silverlight sous Visual Studio 2008.

    Si je ne dis pas de bêtise je dois donc utiliser Silverlight 3.

    Enfin bref j'essaie d'utiliser la propriété Command d'un objet button pour la binder à une fonction.

    Le compilateur m'envoie boulet en m'insultant avec cette phrase :

    The property 'Command' does not exist on the type 'Button' in the XML namespace 'http://schemas.microsoft.com/winfx/2006/xaml/presentation'.
    Dois je créer moi même la DependencyProperty Command à l'objet Button ou me manque t'il quelquechose (Tools, namespace ou autres ...)

    Cordialement

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 754
    Points
    39 754
    Par défaut
    La propriété Command des boutons n'existe que depuis SL 4

    Mais tu dois pouvoir implémenter un CommandButton qui expose cette propriété...

    Regarde cette réponse sur StackOverflow, il y a un exemple
    http://stackoverflow.com/questions/3...502479#3502479

    (par contre l'exemple est pour WPF, en SL il n'y a pas d'interface ICommandSource, ni de propriété CommandTarget ; il faudra donc adapter un peu)

  3. #3
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2003
    Messages
    197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Juillet 2003
    Messages : 197
    Points : 115
    Points
    115
    Par défaut
    Merci de la réponse.

    Je vais tenter de créer moi même mon composant.

    Si certains d'entre vous ont des pistes (a tout hasard)

  4. #4
    Membre régulier
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Juillet 2003
    Messages
    197
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Juillet 2003
    Messages : 197
    Points : 115
    Points
    115
    Par défaut
    Voici le résultat de mon button :

    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
    public class ExtendedButton : Button
        {
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.Register("Command", typeof(ICommand), typeof(ExtendedButton), null);
     
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
     
            public static readonly DependencyProperty CommandParameterProperty =
                DependencyProperty.Register("CommandParameter", typeof(object), typeof(ExtendedButton), null);
     
            public object CommandParameter
            {
                get { return (object)GetValue(CommandParameterProperty); }
                set { SetValue(CommandParameterProperty, value); }
            }
     
            protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
            {
                base.OnMouseLeftButtonUp(e);
     
                ICommand command = this.Command;
                object parameter = this.CommandParameter;
     
                if (command != null && command.CanExecute(parameter))
                    command.Execute(parameter);
            }
        }
    Par contre pour lier mon bouton à une command je fais ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    public ICommand NextQuestionCommand { get; private set; }
    this.NextQuestionCommand = new RelayCommand(NextQuestion, CanNextQuestion);
    public void NextQuestion()
            {
                ...
            }
    public bool  CanNextQuestion()
            {
                return ...
     
            }
    Le bouton fonctionne bien si les conditions de CanNextQuestion ne sont pas remplis alors le bouton ne fait rien sauf que dans SL4, le bouton est directement grisé selon le résultat de la condition.

    Comment puis remettre en place ce système, car pour le coup c'est beaucoup plus simple à gérer après

  5. #5
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Points : 39 754
    Points
    39 754
    Par défaut
    Abonne-toi à l'évènement CanExecuteChanged de la commande. Quand cet évènement se produit, mets juste la valeur de CanExecute dans IsEnabled:

    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
            public static readonly DependencyProperty CommandProperty =
                DependencyProperty.Register("Command", typeof(ICommand), typeof(ExtendedButton), new PropertyMetadata(null, OnCommandChanged));
     
            public ICommand Command
            {
                get { return (ICommand)GetValue(CommandProperty); }
                set { SetValue(CommandProperty, value); }
            }
     
            private static void OnCommandChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                ExtendedButton button = o as ExtendedButton;
                if (button == null)
                    return;
     
                ICommand oldCommand = (ICommand)e.OldValue;
                ICommand newCommand = (ICommand)e.NewValue;
     
                // Désabonnement de l'ancienne commande
                if (oldCommand != null)
                    oldCommand.CanExecuteChanged -= button.command_CanExecuteChanged;
     
                // Abonnement de la nouvelle commande
                if (newCommand != null)
                    newCommand.CanExecuteChanged += button.command_CanExecuteChanged;
            }
     
     
            private void command_CanExecuteChanged(object sender, EventArgs e)
            {
                this.IsEnabled = this.Command == null || Command.CanExecute(CommandParameter);
            }

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

Discussions similaires

  1. [1.x] The "default" context does not exist.
    Par hou1919 dans le forum Symfony
    Réponses: 2
    Dernier message: 12/05/2011, 14h19
  2. erreur: property PrinterModeDraft does not exist
    Par gabar dans le forum Langage
    Réponses: 1
    Dernier message: 08/06/2010, 20h31
  3. The name 'AdressFamily' does not exist in the current context
    Par konamine dans le forum Windows Forms
    Réponses: 1
    Dernier message: 26/10/2008, 16h49
  4. Problème "The specified service does not exist as an ..
    Par Rimak2 dans le forum MS SQL Server
    Réponses: 4
    Dernier message: 23/05/2005, 21h24
  5. FATAL 1: Database "x" does not exist in the syste
    Par barbituric dans le forum PostgreSQL
    Réponses: 11
    Dernier message: 17/03/2004, 06h35

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