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 :

Binding et Template


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 3
    Par défaut Binding et Template
    Voilà je m'essaye à WPF mais j'ai encore pas mal de soucis.
    Ma window est classique, type formulaire d'inscription, avec des textbox utilisant des templates.
    J'ai eu beau chercher, je ne comprends pas pourquoi le Binding de mes textbox est inopérant lorsque je les associe au Template, mais fonctionne sans.

    Si quelqu'un pouvait m'éclairer, voir connait un tuto je suis preneur.

    Voici le code 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
    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
    <Window x:Class="FolkloreWPF.Inscription"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:validators="clr-namespace:MesValidators"
        Title="Inscription" Height="480" Width="640" Loaded="Window_Loaded">
        
        <Grid x:Name="LayoutRoot">
            <Grid.Background>
                <ImageBrush ImageSource="Images/REDIceFlourish.jpg" Stretch="Fill"/>
            </Grid.Background>
            <Grid Margin="20,9.5,92,67" Name="mainGrid" VerticalAlignment="Stretch" Height="360" Width="400" HorizontalAlignment="Left">
                <Grid.RowDefinitions>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                    <RowDefinition></RowDefinition>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleNom" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectanglePrenom" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="1" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectanglePseudo" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="2" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectanglePassword" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="3" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleAdresse" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="4" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleTel" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="5" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleFax" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="6" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleMail" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="7" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleDate" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="8" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleImage" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="9" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleCivilite" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="10" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
                <Rectangle RenderTransformOrigin="0.5,0.5" x:Name="rectangleProfil" Fill="#FF641515" StrokeThickness="0" RadiusX="8" RadiusY="8" Grid.ColumnSpan="2" Grid.Row="11" VerticalAlignment="Center" HorizontalAlignment="Center" Width="375" Height="26" />
    
                <Label Name="labelNom" HorizontalAlignment="Stretch" Grid.Row="0" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Nom :</Label>
                <Label Name="labelPrenom" HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Prénom :</Label>
                <Label Name="labelPseudo" HorizontalAlignment="Stretch" Grid.Row="2" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Pseudo :</Label>
                <Label Name="labelPassword" HorizontalAlignment="Stretch" Grid.Row="3" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Password :</Label>
                <Label Name="labelAdresse" HorizontalAlignment="Stretch" Grid.Row="4" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Adresse :</Label>
                <Label Name="labelTel" HorizontalAlignment="Stretch" Grid.Row="5" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Téléphone :</Label>
                <Label Name="labelFax" HorizontalAlignment="Stretch" Grid.Row="6" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Fax :</Label>
                <Label Name="labelMail" HorizontalAlignment="Stretch" Grid.Row="7" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Mail :</Label>
                <Label Name="labelDateNaissance" HorizontalAlignment="Stretch" Grid.Row="8" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Date de Naissance :</Label>
                <Label Name="labelImage" HorizontalAlignment="Stretch" Grid.Row="9" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Image :</Label>
                <Label Name="labelCivilite" HorizontalAlignment="Stretch" Grid.Row="10" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Civilité :</Label>
                <Label Name="labelProfil" HorizontalAlignment="Stretch" Grid.Row="11" Grid.Column="0" Padding="30 5" Foreground="White" VerticalContentAlignment="Center">Profil :</Label>
                <TextBox Name="txtNom" Grid.Column="1" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" Text="{Binding Path=Nom}" />
                <TextBox Name="txtPrenom" Grid.Column="1" Grid.Row="1" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}">
                    <TextBox.Text>
                        <Binding Path="Prenom" UpdateSourceTrigger="PropertyChanged">
                            <Binding.ValidationRules>
                                <validators:nomRule />
                            </Binding.ValidationRules>
                        </Binding>
                    </TextBox.Text>
                </TextBox>
                <TextBox Name="txtPseudo" Grid.Column="1" Grid.Row="2" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <PasswordBox Name="txtPassword" Grid.Column="1" Grid.Row="3" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource passwordBox}" />
                <TextBox Name="txtAdresse" Grid.Column="1" Grid.Row="4" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <TextBox Name="txtTelephone" Grid.Column="1" Grid.Row="5" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <TextBox Name="txtFax" Grid.Column="1" Grid.Row="6" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <TextBox Name="txtMail" Grid.Column="1" Grid.Row="7" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <TextBox Name="txtDateNaissance" Grid.Column="1" Grid.Row="8" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <TextBox Name="txtImage" Grid.Column="1" Grid.Row="9" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <TextBox Name="txtCivilite" Grid.Column="1" Grid.Row="10" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" />
                <TextBox Name="txtProfil" Grid.Column="1" Grid.Row="11" Margin="0,0,24,0"  VerticalAlignment="Center" HorizontalAlignment="Left" Width="150" Template="{StaticResource textBox}" IsReadOnly="True" />
            </Grid>
            <Grid Margin="430,350,0,0" Name="boutonGrid" VerticalAlignment="Stretch" Height="80" Width="80" HorizontalAlignment="Left">
                <Button Height="80" Width="80" HorizontalAlignment="Center" Name="btnValider" VerticalAlignment="Center" Template="{StaticResource bouton}" Click="btnValider_Click">Valider</Button>
            </Grid>
        </Grid>
    </Window>
    La ressource template :
    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
    <Application x:Class="FolkloreWPF.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        StartupUri="WindowLogin.xaml">
        <Application.Resources>
            <ControlTemplate x:Key="textBox" TargetType="{x:Type TextBox}">
                <Grid>
                    <TextBox x:Name="textBox" Width="150" Height="Auto" Background="#FFBA3E3E" RenderTransformOrigin="0.5,0.5">
                    </TextBox>
                    <ContentPresenter RecognizesAccessKey="True" VerticalAlignment="center" HorizontalAlignment="center"/>
                </Grid>
                <ControlTemplate.Triggers>
                    <EventTrigger RoutedEvent="FocusManager.GotFocus" SourceName="textBox">
                        <BeginStoryboard>
                            <Storyboard>
                                <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="textBox" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                    <SplineColorKeyFrame KeyTime="00:00:01" Value="#FFEDA0A0"/>
                                    <SplineColorKeyFrame KeyTime="00:00:02" Value="#FFBA3E3E"/>
                                </ColorAnimationUsingKeyFrames>
                            </Storyboard>
                        </BeginStoryboard>
                    </EventTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Application.Resources>
    </Application>
    et le code-behind :
    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
    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.Shapes;
    using ObjetPersonnes;
     
     
    namespace FolkloreWPF
    {
        /// <summary>
        /// Logique d'interaction pour Inscription.xaml
        /// </summary>
        public partial class Inscription : Window
        {
     
            Personnes newPersonne = new Personnes();
     
            public Inscription()
            {
                InitializeComponent();
            }
     
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                this.DataContext = newPersonne;
            }    
     
            private void btnValider_Click(object sender, RoutedEventArgs e)
            {
                //test string pour le binding
                string str = string.Format("OBJET:\nNom:{0}"
                    +"\nPrenom:{1}",
                    newPersonne.Nom,
                    newPersonne.Prenom);
                MessageBox.Show(str);
            }
        }
    }

  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 un TextBox, le ControlTemplate doit respecter certaines normes :
    http://msdn.microsoft.com/en-us/library/ms752068.aspx

    The ControlTemplate for a TextBox must contain exactly one element that is tagged as the content host element; this element will be used to render the contents of the TextBox. To tag an element as the content host, assign it the special name PART_ContentHost. The content host element must be either a ScrollViewer or an AdornerDecorator. The content host element may not host any child elements.
    En résumé, il doit contenir un ScrollViewer ou Decorator que tu dois nommé PART_ContentHost.

    Pour ton exemple ça pourrait donner :
    Code xml : 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
        <ControlTemplate x:Key="textBox" TargetType="{x:Type TextBox}">
          <Border x:Name="Bd" Background="Orange" >
            <ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                          x:Name="PART_ContentHost" VerticalAlignment="Center" />
          </Border>
          <ControlTemplate.Triggers>
            <EventTrigger RoutedEvent="FocusManager.GotFocus" SourceName="Bd">
              <BeginStoryboard>
                <Storyboard>
                  <ColorAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Bd" Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                    <SplineColorKeyFrame KeyTime="00:00:01" Value="#FFEDA0A0"/>
                    <SplineColorKeyFrame KeyTime="00:00:02" Value="#FFBA3E3E"/>
                  </ColorAnimationUsingKeyFrames>
                </Storyboard>
              </BeginStoryboard>
            </EventTrigger>
          </ControlTemplate.Triggers>
        </ControlTemplate>

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Janvier 2009
    Messages
    3
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2009
    Messages : 3
    Par défaut
    Merci pour ta réponse rapide, ça marche super désormais.

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

Discussions similaires

  1. Binding de template
    Par redcurve dans le forum AngularJS
    Réponses: 4
    Dernier message: 13/11/2014, 08h19
  2. binding c++ template class luabind
    Par Bob.Killer dans le forum C++
    Réponses: 0
    Dernier message: 10/04/2014, 19h54
  3. bind méthode template
    Par Lintel-oo dans le forum Langage
    Réponses: 9
    Dernier message: 27/10/2012, 17h56
  4. Bind d'une méthode template privée
    Par kidpaddle2 dans le forum Boost
    Réponses: 2
    Dernier message: 29/06/2010, 16h32
  5. Bind, thread et membre template
    Par olivier1978 dans le forum Threads & Processus
    Réponses: 8
    Dernier message: 14/11/2007, 19h19

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