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 :

XAML - Récupérer une instance créée en code-behind


Sujet :

Windows Presentation Foundation

  1. #1
    Membre actif
    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
    Points : 265
    Points
    265
    Par défaut XAML - Récupérer une instance créée en code-behind
    Bonjour,
    je me perds une peu dans le binding... J'ai créé un projet de test tout simple : une classe Personne (Nom, Prenom, Age) et une window avec 3 textbox bindées aux 3 propriétés.
    Ce que j'arrive à faire :

    1/ Créer une instance de ma classe Personne en code-behind, et définir également en code-behind la DataContext de ma window => ça marche

    2/ Créer une instance de ma classe Personne et définir le DataContext, le tout en Xaml => ça marche

    MAIS :
    comment récupérer en Xaml, une instance de ma classe Personne créée en code-behind. J'ai défini au niveau de ma window, une variable publique nommée MyPerson = New Personne ("Nom", "Prénom", 30). Comment la référencer au niveau du Xaml pour pouvoir définir le DataContext en Xaml et non pas dans le code-behind ?

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

    Informations forums :
    Inscription : Octobre 2002
    Messages : 9 161
    Points : 19 434
    Points
    19 434
    Par défaut
    Il faut que tu la mettes en static et là, tu pourras y accéder depuis ton code XAML

  3. #3
    Membre habitué Avatar de Thrud
    Profil pro
    Développeur .NET
    Inscrit en
    Avril 2008
    Messages
    170
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Avril 2008
    Messages : 170
    Points : 183
    Points
    183
    Par défaut
    Sans passer par une variable statique, si c'est une propriété LaPersonne dans ta Window1, tu peux aussi y faire référence dans ton xaml de cette façon :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <Window x:Class="MyProject.Window1" x:Name="TheWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    
    Title="Window" Height="500" Width="700">
    ...
    <TextBoxText="{Binding Path=LaPersonne.Name, ElementName=TheWindow}" />
     
    

  4. #4
    Membre actif
    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
    Points : 265
    Points
    265
    Par défaut
    Désolé mais ça ne marche pas. Voici le code vb:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    Class Window1 
        Public MyPerson As New Personne("Tartampion", "Fred", 38)
     
        Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles btnOK.Click
            Me.Close()
        End Sub
    End Class
    Et voici le xaml :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <Window x:Class="Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:tests="clr-namespace:Tests"
        Title="Window1" Height="386" Width="650" Name="Window1" WindowStartupLocation="CenterScreen">
     
        <Grid>
            <TextBox Height="25" Margin="120,30,304,0" Name="txtNom" VerticalAlignment="Top" Text="{Binding ElementName=Window1, Path=MyPerson.Nom}"/>
     
            <Button Height="39" HorizontalAlignment="Left" Margin="29,0,0,98" Name="btnOK" VerticalAlignment="Bottom" Width="102">OK</Button>
        </Grid>
    </Window>
    Compilation ok, mais rien ne s'affiche...

  5. #5
    Membre habitué Avatar de Thrud
    Profil pro
    Développeur .NET
    Inscrit en
    Avril 2008
    Messages
    170
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Avril 2008
    Messages : 170
    Points : 183
    Points
    183
    Par défaut
    euh...

    Sans passer par une variable statique, si c'est une propriété LaPersonne dans ta Window1...
    Si c'est une Propriété ! je ne sais pas comment tu déclare une proprité en VB, en C#, ça donnerait un code comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    // Une variable membre
    private Personne _personne = new Personne("Tartampion", "Fred", 38);
     
    // Et une propriété
    public Personne MyPerson 
    {
       get { return _personne; }
       set { _personne = value; }
    }

  6. #6
    Membre actif
    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
    Points : 265
    Points
    265
    Par défaut
    OK. Je n'avais pas bien lu ta réponse. J'avais juste déclarer mon instance en tant que variable publique et non pas propriété.
    Merci.

  7. #7
    Membre habitué Avatar de Thrud
    Profil pro
    Développeur .NET
    Inscrit en
    Avril 2008
    Messages
    170
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Avril 2008
    Messages : 170
    Points : 183
    Points
    183
    Par défaut
    Y'a pas de mal, et ça marche comme ça ?

  8. #8
    Membre actif
    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
    Points : 265
    Points
    265
    Par défaut
    Ca marche nickel.

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

Discussions similaires

  1. Récupérer une variable javascript dans code behind c#
    Par zakarinalaw dans le forum jQuery
    Réponses: 4
    Dernier message: 17/02/2014, 08h39
  2. Creation d'une page entierement en code behind
    Par superfly dans le forum ASP.NET
    Réponses: 2
    Dernier message: 13/09/2007, 17h52
  3. Réponses: 4
    Dernier message: 27/07/2007, 20h34
  4. Récupérer une instance d'un objet
    Par MDiabolo dans le forum MFC
    Réponses: 9
    Dernier message: 26/01/2007, 10h41
  5. Comment récupérer une instance de Graphics::TGraphic ?
    Par Invité dans le forum C++Builder
    Réponses: 2
    Dernier message: 11/12/2006, 15h01

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