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 :

code XAML équivalent au code Behind d'un binding


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Par défaut code XAML équivalent au code Behind d'un binding
    Bonjour,

    Pourriez-vous me donner l'équivalent de ce code Behind d'un binding en XAML?

    voici le code de mon UserControl. Je lie le texte de ma textbox à ma Dependency Property Valeur.

    Code Behind
    --------------
    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
    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
     
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;
     
    namespace Verification_Lotto
    {
        /// <summary>
        /// Interaction logic for NumericUpDown.xaml
        /// </summary>
        public partial class NumericUpDown : UserControl
        {
     
            Binding bind;
     
            public int Valeur
            {
                get { return (int)GetValue(ValeurProperty); }
                set { SetValue(ValeurProperty, value); }
            }
     
            public static readonly DependencyProperty ValeurProperty =
                DependencyProperty.Register("Valeur", typeof(int), typeof(NumericUpDown), new UIPropertyMetadata(0, onValeurPropertyChanged ));
     
            private static void onValeurPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
            {
     
            }
     
            public NumericUpDown()
            {
                InitializeComponent();
     
                bind = new Binding();
                bind.Source = this;
                bind.Mode = BindingMode.TwoWay;
                bind.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
                bind.Path = new PropertyPath("Valeur");
                this.txtNum.SetBinding(TextBox.TextProperty, bind);
     
            }
     
            private void btPlus_Click(object sender, RoutedEventArgs e)
            {
                if (this.Valeur < 42)
                {
                    this.Valeur = this.Valeur + 1;
                }
                /*txtNum.Text = this.Valeur.ToString();*/
            }
     
            private void btMoins_Click(object sender, RoutedEventArgs e)
            {
                if (this.Valeur > 1)
                {
                    this.Valeur = this.Valeur - 1;
                }
                /*txtNum.Text = this.Valeur.ToString();*/
            }
     
            private void txtNum_PreviewKeyDown(object sender, KeyEventArgs e)
            {
                Key tmp = e.Key;
                if (!(tmp.ToString() == "D1" || tmp.ToString() == "NumPad1" ||
                      tmp.ToString() == "D2" || tmp.ToString() == "NumPad2" ||
                      tmp.ToString() == "D3" || tmp.ToString() == "NumPad3" ||
                      tmp.ToString() == "D4" || tmp.ToString() == "NumPad4" ||
                      tmp.ToString() == "D5" || tmp.ToString() == "NumPad5" ||
                      tmp.ToString() == "D6" || tmp.ToString() == "NumPad6" ||
                      tmp.ToString() == "D7" || tmp.ToString() == "NumPad7" ||
                      tmp.ToString() == "D8" || tmp.ToString() == "NumPad8" ||
                      tmp.ToString() == "D9" || tmp.ToString() == "NumPad9" ||
                      tmp.ToString() == "D0" || tmp.ToString() == "NumPad0" ||
                      tmp.ToString() == "Back" || tmp.ToString() == "Return" || 
                      tmp.ToString() == "Enter" || tmp.ToString() == "Tab"))
                {
                    e.Handled = true;
                }
            }
     
            private void txtNum_LostFocus(object sender, RoutedEventArgs e)
            {
                if (txtNum.Text == "" || txtNum.Text == " ")
                {
                    txtNum.Text = "1";
                }
            }
     
        }
    }

    Code Xaml
    ------------
    Code XAML : 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="Verification_Lotto.NumericUpDown"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="24" Width="100">
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="75"></ColumnDefinition>
                <ColumnDefinition></ColumnDefinition>
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
     
            <TextBox Name="txtNum" Grid.Column="0" Grid.Row="0" Grid.RowSpan="2" PreviewKeyDown="txtNum_PreviewKeyDown" LostFocus="txtNum_LostFocus"></TextBox>
            <RepeatButton Name="btPlus" Grid.Column="1" Grid.Row="0" Content="+" FontSize="8" VerticalContentAlignment="Top" Click="btPlus_Click" Padding="0"></RepeatButton>
            <RepeatButton Name="btMoins" Grid.Column="1" Grid.Row="1" Content="-" FontSize="8" VerticalContentAlignment="Top" Click="btMoins_Click" Padding="0"></RepeatButton>
     
        </Grid>
    </UserControl>


    Un grand merci.

  2. #2
    Membre éclairé
    Profil pro
    Inscrit en
    Août 2006
    Messages
    582
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2006
    Messages : 582
    Par défaut
    Si ton UserControl possède comme Dependency Property Valeur, alors voici le binding en xaml :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    <Textbox Text="{Binding Valeur, ElementName=userControl}"/>

    P.S : dans le Xaml de ton UserControl, il faut rajouter :
    Code xaml : Sélectionner tout - Visualiser dans une fenêtre à part
    <UserControl x:Name="userControl" x:Class="Verification_Lotto.NumericUpDown" [...]

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Février 2009
    Messages
    171
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Février 2009
    Messages : 171
    Par défaut
    Impeccable ça fonctionne.

    Merci

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

Discussions similaires

  1. [Débutant] Problème compréhension code XAML
    Par JolyLoic dans le forum Windows Presentation Foundation
    Réponses: 3
    Dernier message: 26/08/2009, 17h00
  2. Utilisation d'un dependency propeties dans le code XAML
    Par usbeck dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 13/01/2009, 10h39
  3. Voir l'équivalent en code sql
    Par Tsukaasa dans le forum Access
    Réponses: 2
    Dernier message: 02/09/2008, 02h43
  4. Ajout de code html depuis le code behind
    Par Apo94 dans le forum ASP.NET
    Réponses: 6
    Dernier message: 06/05/2008, 08h38

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