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 :

[WPF] UserControl et Click


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2007
    Messages
    634
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2007
    Messages : 634
    Par défaut [WPF] UserControl et Click
    Bonjour à tous,

    je viens de créer un user controle contenant entre autre un textblock et un bouton avec une image à l'interieur. J'ai rajouté des DependencyProperty telle que Image pour setter l'image du bouton et Text pour setter le textblock. Je shouterais ajouter l'évènement click a mon controle aui serai directement linker avec le click du boutton, comment puis-je faire ?

    Merci d'avance,

    Cordialement
    NeoKript

  2. #2
    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
    Salut,

    Pour le click sur le bouton, tu peux utiliser la propriété Command du bouton.

    Un exemple de création de commande :
    http://msdn.microsoft.com/en-us/libr...edcommand.aspx

    Tu peux donc créer une DependencyProperty ButtonCommand sur le même principe que tes autres DependencyProperty.

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

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

    Informations forums :
    Inscription : Juillet 2007
    Messages : 576
    Par défaut
    Ou alors tu peux aussi utiliser la méthode décrite sur ce blog :

    http://wcode.net/2009/08/exposing-in...f-usercontrol/

  4. #4
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2007
    Messages
    634
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2007
    Messages : 634
    Par défaut
    Salut et merci pour vos réponses.

    Avec les DependencyProperty je ne vois pas comment faire.
    J'ai fais ceci mais on ne peut pas binder le click visiblement :

    .xaml
    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
     
    <UserControl 
        x:Class="DesktopBrowser.UserControls.ButtonMenu"
        x:Name="Button"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="140" Width="100">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition Height="40"/>
            </Grid.RowDefinitions>
            <Button Grid.Column="0" Grid.Row="0" Margin="10" Click="{Binding ElementName=Button, Path=Click}">
                <Image Source="{Binding ElementName=Button, Path=Image}" />
            </Button>
            <TextBlock Grid.Column="0" Grid.Row="1" Text="{Binding ElementName=Button, Path=Text}" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="14" FontWeight="Bold" />
        </Grid>
    </UserControl>
    .cs
    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
     
    public partial class ButtonMenu : UserControl
        {
            public ButtonMenu()
            {
                InitializeComponent();
            }
     
            public static DependencyProperty TextProperty = DependencyProperty.Register("Text", typeof(String), typeof(ButtonMenu));
            public static DependencyProperty ImageProperty = DependencyProperty.Register("Image", typeof(ImageSource), typeof(ButtonMenu));
            public static DependencyProperty ClickProperty = DependencyProperty.Register("Click", typeof(RoutedEventHandler), typeof(ButtonMenu));
     
            public RoutedEventHandler Click
            {
                get { return (RoutedEventHandler)(this.GetValue(ClickProperty)); }
                set { this.SetValue(ClickProperty, (RoutedEventHandler)value); }
            }
     
            public ImageSource Image
            {
                get { return (ImageSource)(this.GetValue(ImageProperty)); }
                set { this.SetValue(ImageProperty, (ImageSource)value); }
            }
     
            public String Text
            {
                get { return (String)(this.GetValue(TextProperty)); }
                set { this.SetValue(TextProperty, (String)value); }
            }
        }
    Si tu pouvais m'expliquer.
    Cordialement,
    Neokript

  5. #5
    Membre éclairé
    Homme Profil pro
    Étudiant
    Inscrit en
    Novembre 2007
    Messages
    634
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Loire (Rhône Alpes)

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Novembre 2007
    Messages : 634
    Par défaut
    J'ai utilisé la méthode décrite dans le site qu'à donné jmix90.
    Ça marche nickel.

    Merci encore !

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

Discussions similaires

  1. Problème sur le WPF Toolkit DataGrid click droit
    Par azar121 dans le forum Windows Presentation Foundation
    Réponses: 7
    Dernier message: 03/04/2014, 11h16
  2. [WPF] listbox et click sur item
    Par waspy59 dans le forum Windows Presentation Foundation
    Réponses: 23
    Dernier message: 01/03/2010, 00h22
  3. [WPF] UserControl -> binding ??
    Par UNi[FR] dans le forum C#
    Réponses: 1
    Dernier message: 10/09/2007, 11h31
  4. [WPF] UserControl pour un historique de nav
    Par UNi[FR] dans le forum Windows Presentation Foundation
    Réponses: 8
    Dernier message: 10/09/2007, 11h19
  5. [WPF] UserControl: impossible à ajouter à la toolbox
    Par SoBaKa dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 23/05/2007, 09h14

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