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 :

Gérer le double clique à l'aide d'une commande WPF


Sujet :

Windows Presentation Foundation

  1. #1
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 53
    Par défaut Gérer le double clique à l'aide d'une commande WPF
    Bonjour, j'aimerais savoir comment pourrait-on exécuter une commande suite à un double clique sur un bouton (ou sur un autre composant : Label, TextBlock..).

    L'application est développée en M-V-VM.

    Merci de votre aide.

  2. #2
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    Tu peux passer par un Timer qui ne va exécuter le code de ta commande que si le 2ème clic a lieu avant 500 ms.

    Regarde comment on gère le double clic en Silverlight et inspire toi de la technique

  3. #3
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 53
    Par défaut
    Il n'y a pas un autre moyen de faire avec les InputBindings et les MouseBinding (donner des exemples de codes svp).
    Aller Thomas, tu m'as donné une solution trop fastidieuse à mettre en oeuvre, en plus je ne suis pas SilverLight ...

    Merci de m'aider.

  4. #4
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    Non, pas d'autres moyens à ma connaissance....

  5. #5
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 53
    Par défaut
    Merci quand même, je vais chercher de mon côté, je vous tiendrai au courant.

  6. #6
    Membre Expert
    Profil pro
    Inscrit en
    Décembre 2004
    Messages
    2 210
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 210
    Par défaut
    Citation Envoyé par Thomas Lebrun
    Non, pas d'autres moyens à ma connaissance....
    Je suis peut-être à côté de la question, mais pour ce que j'en comprend, on peut en effet utiliser les InputBindings :
    http://www.developpez.net/forums/d93...s-click-image/

    Tu proposais d'ailleurs une autre solution : les Behaviors

  7. #7
    Rédacteur
    Avatar de Thomas Lebrun
    Profil pro
    Inscrit en
    Octobre 2002
    Messages
    9 161
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Par défaut
    Citation Envoyé par binoo Voir le message
    Je suis peut-être à côté de la question, mais pour ce que j'en comprend, on peut en effet utiliser les InputBindings :
    http://www.developpez.net/forums/d93...s-click-image/

    Tu proposais d'ailleurs une autre solution : les Behaviors
    Ah oui tiens, je suis un peu à la ramasse ne ce moment moi

  8. #8
    Membre actif
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    69
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 69
    Par défaut
    Salut à tous !

    Juste parce que je souhaitais trouver une méthode pour récupérer le double clic et lancer une commande, comme on le ferait avec un simple clic, je suis tombé sur ce post. Mais juste avant, je suis tombé sur cette page.

    Voilà

    PS : la syntaxe est légèrement différente (propriété Gesture) et permet de capturer par exemple "CTRL+LeftClick"

  9. #9
    Membre actif
    Profil pro
    Inscrit en
    Novembre 2008
    Messages
    53
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Novembre 2008
    Messages : 53
    Par défaut
    Merci bien MiniCesc, ton lien m'a permis de me rafraîchir un peu la mémoire concernant les InputBinding.

    En plus ça tombe vraiment bien, car je suis actuellement entrain de chercher à binder un CheckBox à une commande, je m'explique :

    Sous le modèle MVVM, j'ai un DataGrid qui affiche un ensemble de sites (Nom, Adresse, Ville, CodePostal, TypeSite....), et juste à côté j'ai une ListBox templatée avec un CheckBox et un Label (via DataTempalte), La ListeBox contient l'ensemble les types de sites (Touristique, Archéologique, Historique...etc).

    J'aimerais pouvoir déclencher une commande lorsque je clique sur le checkBox d'un ListBoxItem pour trier mes sites selon les types sélectionnés (cochés).
    Mon problème est que je n'arrive pas à récupérer le CurrentItem des types de sites.

    J'espère avoir été clair, merci pour vos suggestions.
    Cordialement.

  10. #10
    Membre actif
    Profil pro
    Inscrit en
    Septembre 2010
    Messages
    69
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2010
    Messages : 69
    Par défaut
    Y a pas de quoi

    Bon courage pour tes histoires de tri ^^

  11. #11
    Membre confirmé
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2010
    Messages
    188
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 38
    Localisation : France, Loire Atlantique (Pays de la Loire)

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : Santé

    Informations forums :
    Inscription : Août 2010
    Messages : 188
    Par défaut
    J'ai un problème similaire aussi.
    Une ListView/GridView dans une fenêtre avec un bouton dont le code est le suivant:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    <ListView>
        <ListView.InputBindings>
            <MouseBinding Command="{Binding CommandOK}" MouseAction="LeftDoubleClick" />
        </ListView.InputBindings>
    </ListView>
    <Button Command="{Binding CommandOK}" />
    Le bouton fonctionne mais pas le MouseBinding.

    Par contre dans une autre partie de mon logiciel j'ai sur un UserControl écrit comme ceci
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    <myUserControl:UC>
        <myUserControl:UC.InputBindings>
            <MouseBinding Command="{myInput:CommandBinding MyCommand}" MouseAction="LeftClick" />
        </myUserControl:UC.InputBindings>
    </myUserControl:UC>
    mon UserControl est utiliser plusieurs fois dans une même fenêtre et comprend en fait un border qui change de BorderThickness selon si j'ai clicker dessus ou pas.

    Cette partie utilise une classe qui est la suivante que j'ai trouver sur le net
    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
    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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    using System;
    using System.Reflection;
    using System.Windows;
    using System.Windows.Input;
    using System.Windows.Markup;
     
    namespace MonProjet
    {
        [MarkupExtensionReturnType(typeof(ICommand))]
        public class CommandBindingExtension : MarkupExtension
        {
            public CommandBindingExtension()
            {
            }
     
            public CommandBindingExtension(string commandName)
            {
                this.CommandName = commandName;
            }
     
            [ConstructorArgument("commandName")]
            public string CommandName { get; set; }
     
            private object targetObject;
            private object targetProperty;
     
            public override object ProvideValue(IServiceProvider serviceProvider)
            {
                IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
                if (provideValueTarget != null)
                {
                    targetObject = provideValueTarget.TargetObject;
                    targetProperty = provideValueTarget.TargetProperty;
                }
     
                if (!string.IsNullOrEmpty(CommandName))
                {
                    // The serviceProvider is actually a ProvideValueServiceProvider, which has a private field "_context" of type ParserContext
                    ParserContext parserContext = GetPrivateFieldValue<ParserContext>(serviceProvider, "_context");
                    if (parserContext != null)
                    {
                        // A ParserContext has a private field "_rootElement", which returns the root element of the XAML file
                        FrameworkElement rootElement = GetPrivateFieldValue<FrameworkElement>(parserContext, "_rootElement");
                        if (rootElement != null)
                        {
                            // Now we can retrieve the DataContext
                            object dataContext = rootElement.DataContext;
     
                            // The DataContext may not be set yet when the FrameworkElement is first created, and it may change afterwards,
                            // so we handle the DataContextChanged event to update the Command when needed
                            if (!dataContextChangeHandlerSet)
                            {
                                rootElement.DataContextChanged += new DependencyPropertyChangedEventHandler(rootElement_DataContextChanged);
                                dataContextChangeHandlerSet = true;
                            }
     
                            if (dataContext != null)
                            {
                                ICommand command = GetCommand(dataContext, CommandName);
                                if (command != null)
                                    return command;
                            }
                        }
                    }
                }
     
                // The Command property of an InputBinding cannot be null, so we return a dummy extension instead
                return DummyCommand.Instance;
            }
     
            private ICommand GetCommand(object dataContext, string commandName)
            {
                PropertyInfo prop = dataContext.GetType().GetProperty(commandName);
                if (prop != null)
                {
                    ICommand command = prop.GetValue(dataContext, null) as ICommand;
                    if (command != null)
                        return command;
                }
                return null;
            }
     
            private void AssignCommand(ICommand command)
            {
                if (targetObject != null && targetProperty != null)
                {
                    if (targetProperty is DependencyProperty)
                    {
                        DependencyObject depObj = targetObject as DependencyObject;
                        DependencyProperty depProp = targetProperty as DependencyProperty;
                        depObj.SetValue(depProp, command);
                    }
                    else
                    {
                        PropertyInfo prop = targetProperty as PropertyInfo;
                        prop.SetValue(targetObject, command, null);
                    }
                }
            }
     
            private bool dataContextChangeHandlerSet = false;
            private void rootElement_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
            {
                FrameworkElement rootElement = sender as FrameworkElement;
                if (rootElement != null)
                {
                    object dataContext = rootElement.DataContext;
                    if (dataContext != null)
                    {
                        ICommand command = GetCommand(dataContext, CommandName);
                        if (command != null)
                        {
                            AssignCommand(command);
                        }
                    }
                }
            }
     
            private T GetPrivateFieldValue<T>(object target, string fieldName)
            {
                FieldInfo field = target.GetType().GetField(fieldName, BindingFlags.Instance | BindingFlags.NonPublic);
                if (field != null)
                {
                    return (T)field.GetValue(target);
                }
                return default(T);
            }
     
            // A dummy command that does nothing...
            private class DummyCommand : ICommand
            {
     
                #region Singleton pattern
     
                private DummyCommand()
                {
                }
     
                private static DummyCommand _instance = null;
                public static DummyCommand Instance
                {
                    get
                    {
                        if (_instance == null)
                        {
                            _instance = new DummyCommand();
                        }
                        return _instance;
                    }
                }
     
                #endregion
     
                #region ICommand Members
     
                public bool CanExecute(object parameter)
                {
                    return false;
                }
     
                public event EventHandler CanExecuteChanged;
     
                public void Execute(object parameter)
                {
                }
     
                #endregion
            }
        }
    }
    En espérant que cela t'aide d'ailleurs je vais replonger ca résoudra peut-être mon problème

Discussions similaires

  1. Aide pour une commande dynamique
    Par guiblan dans le forum Langage
    Réponses: 1
    Dernier message: 28/08/2009, 04h28
  2. remplir un rapport à l'aide d'une commande
    Par asprog dans le forum Windows Forms
    Réponses: 11
    Dernier message: 23/07/2009, 01h06
  3. aide sur une commande
    Par xoum89 dans le forum SQL
    Réponses: 6
    Dernier message: 06/05/2009, 19h39
  4. lancer une application en pascal a l'aide d'une commande en vb
    Par SOU_MA dans le forum VB 6 et antérieur
    Réponses: 0
    Dernier message: 25/11/2008, 15h47
  5. Besoin d'aide pour une commande
    Par maxwell86 dans le forum PL/SQL
    Réponses: 0
    Dernier message: 20/10/2008, 11h44

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