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 :

Icommand MVVM parametres


Sujet :

Windows Presentation Foundation

  1. #1
    Membre régulier
    Inscrit en
    Mai 2007
    Messages
    137
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 137
    Points : 71
    Points
    71
    Par défaut Icommand MVVM parametres
    Bonjour,

    J'ai des radio bouttons dans lesquels j'utilise "Command"

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <RadioButton x:Name="optTyp2" Grid.Column="0" GroupName="Op2" Content="Tous les" IsChecked="{Binding Path=SelectedAction.Frequence_type, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=1}" Command="{Binding TousLesJSMACommand}"/>
    Quand je clique, il effectue bien la commande "TousLesJSMACommand" dans le viewmodel.

    Je voudrais juste pouvoir passer un paramêtre à ma commande pour le récupérer dans le view model. Comme ca, si j'ai 3 radio bouton, je peux utiliser la même commande :

    Rbouton1 Command="{Binding TousLesJSMACommand(0)}"
    Rbouton2 Command="{Binding TousLesJSMACommand(1)}"
    Rbouton3 Command="{Binding TousLesJSMACommand(2)}"

    Voilà le code dans le viewModel :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    private RelayCommand tousLesJSMACommand;
            public ICommand TousLesJSMACommand
            {
                get { return tousLesJSMACommand ?? (tousLesJSMACommand = new RelayCommand(() => TousLesJSMAAction())); }
            }
     
            private void TousLesJSMAAction()
            {
                EnabledJSMA = true;
                EnabledJours = false;
            }
    Merci d'avance

    Gridin

  2. #2
    Rédacteur
    Avatar de Nathanael Marchand
    Homme Profil pro
    Expert .Net So@t
    Inscrit en
    Octobre 2008
    Messages
    3 615
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Expert .Net So@t
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2008
    Messages : 3 615
    Points : 8 080
    Points
    8 080
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <RadioButton x:Name="optTyp2" Grid.Column="0" GroupName="Op2" Content="Tous les" IsChecked="{Binding Path=SelectedAction.Frequence_type, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=1}" Command="{Binding TousLesJSMACommand}" CommandParameter="0" />
    <RadioButton x:Name="optTyp2" Grid.Column="0" GroupName="Op2" Content="Tous les" IsChecked="{Binding Path=SelectedAction.Frequence_type, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=1}" Command="{Binding TousLesJSMACommand}" CommandParameter="1" />
    <RadioButton x:Name="optTyp2" Grid.Column="0" GroupName="Op2" Content="Tous les" IsChecked="{Binding Path=SelectedAction.Frequence_type, Mode=TwoWay, Converter={StaticResource enumConverter}, ConverterParameter=1}" Command="{Binding TousLesJSMACommand}" CommandParameter="2" />

  3. #3
    Membre régulier
    Inscrit en
    Mai 2007
    Messages
    137
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 137
    Points : 71
    Points
    71
    Par défaut
    Cool mais comment je le récupère dans le Model View ?

  4. #4
    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 749
    Points
    39 749
    Par défaut
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
            private RelayCommand<int> tousLesJSMACommand;
            public ICommand TousLesJSMACommand
            {
                get { return tousLesJSMACommand ?? (tousLesJSMACommand = new RelayCommand<int>(TousLesJSMAAction)); }
            }
     
            private void TousLesJSMAAction(int param)
            {
                EnabledJSMA = true;
                EnabledJours = false;
            }

  5. #5
    Membre régulier
    Inscrit en
    Mai 2007
    Messages
    137
    Détails du profil
    Informations forums :
    Inscription : Mai 2007
    Messages : 137
    Points : 71
    Points
    71
    Par défaut
    Désolé mais , je ne comprend pas bien ces ICommand
    Qu'est ce que je dois adapter ici ?


    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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
     
    using System;
    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
    using System.Windows.Input;
     
        public class RelayCommand : ICommand
        {
            private readonly Action _execute;
     
            private readonly Func<bool> _canExecute;
     
            /// <summary>
            /// Initializes a new instance of the RelayCommand class that 
            /// can always execute.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
            public RelayCommand(Action execute)
                : this(execute, null)
            {
            }
     
            public RelayCommand(Action execute, Func<bool> canExecute)
            {
                if (execute == null)
                {
                    throw new ArgumentNullException("execute");
                }
     
                this._execute = execute;
                this._canExecute = canExecute;
            }
     
            public event EventHandler CanExecuteChanged
            {
                add
                {
                    if (this._canExecute != null)
                    {
                        CommandManager.RequerySuggested += value;
                    }
                }
     
                remove
                {
                    if (this._canExecute != null)
                    {
                        CommandManager.RequerySuggested -= value;
                    }
                }
            }
     
            public void RaiseCanExecuteChanged(bool force)
            {
                if (force)
                {
                    CommandManager.InvalidateRequerySuggested();
                }
            }
     
            [DebuggerStepThrough]
            public bool CanExecute(object parameter)
            {
                return this._canExecute == null ? true : this._canExecute();
            }
     
            public void Execute(object parameter)
            {
                this._execute();
            }
     
        }

  6. #6
    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 : 37
    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
    Citation Envoyé par gridin Voir le message
    Désolé mais , je ne comprend pas bien ces ICommand
    Qu'est ce que je dois adapter ici ?
    Hello,

    Tu remplaces partout "Action" par "Action<Object>" et tu remplaces la méthode Execute par celle-ci:
    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
       public void Execute(object parameter)
            {
                this._execute(parameter);
            }

    Bon courage,
    Jonathan ANTOINE - Découvrez mon livre: MVVM, de la découverte à la maîtrise.

    Microsoft MVP Client Application Development
    - MCPD Windows 4.0, etc.
    Mon blog : http://www.jonathanantoine.com

  7. #7
    Rédacteur
    Avatar de Nathanael Marchand
    Homme Profil pro
    Expert .Net So@t
    Inscrit en
    Octobre 2008
    Messages
    3 615
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Expert .Net So@t
    Secteur : Conseil

    Informations forums :
    Inscription : Octobre 2008
    Messages : 3 615
    Points : 8 080
    Points
    8 080
    Par défaut
    Pas testé et fait sur le pouce:
    Mais c'est déjà dispo je crois dans le MvvmLightToolkit et PRISM
    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
    public class RelayCommand<T> : ICommand
        {
            private readonly Action<T> _execute;
     
            private readonly Func<T,bool> _canExecute;
     
            /// <summary>
            /// Initializes a new instance of the RelayCommand class that 
            /// can always execute.
            /// </summary>
            /// <param name="execute">The execution logic.</param>
            /// <exception cref="ArgumentNullException">If the execute argument is null.</exception>
            public RelayCommand<T>(Action<T> execute)
                : this(execute, null)
            {
            }
     
            public RelayCommand<T>(Action<T> execute, Func<T,bool> canExecute)
            {
                if (execute == null)
                {
                    throw new ArgumentNullException("execute");
                }
     
                this._execute = execute;
                this._canExecute = canExecute;
            }
     
            public event EventHandler CanExecuteChanged
            {
                add
                {
                    if (this._canExecute != null)
                    {
                        CommandManager.RequerySuggested += value;
                    }
                }
     
                remove
                {
                    if (this._canExecute != null)
                    {
                        CommandManager.RequerySuggested -= value;
                    }
                }
            }
     
            public void RaiseCanExecuteChanged(bool force)
            {
                if (force)
                {
                    CommandManager.InvalidateRequerySuggested();
                }
            }
     
            [DebuggerStepThrough]
            public bool CanExecute(object parameter)
            {
                return this._canExecute == null ? true : this._canExecute((T)parameter);
            }
     
            public void Execute(object parameter)
            {
                this._execute((T)parameter);
            }
     
        }

Discussions similaires

  1. Modèle MVVM - Implémentation ICommand
    Par Robin56 dans le forum Windows Presentation Foundation
    Réponses: 5
    Dernier message: 23/06/2009, 16h00
  2. [VB6] Transférer ".picture" en paramètre
    Par Ricou13 dans le forum VB 6 et antérieur
    Réponses: 3
    Dernier message: 19/11/2002, 08h43
  3. [VB6] [ADO] Procedure stockée : spécifier les paramètres
    Par adepdoom dans le forum VB 6 et antérieur
    Réponses: 2
    Dernier message: 16/10/2002, 10h45
  4. Passage de parametre a une anim Flash 5
    Par debug dans le forum Intégration
    Réponses: 4
    Dernier message: 03/06/2002, 17h59
  5. transmision de tableau en parametre
    Par Horus dans le forum C++Builder
    Réponses: 3
    Dernier message: 16/05/2002, 11h15

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