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 entre 2 contrôles WPF


Sujet :

Windows Presentation Foundation

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif Avatar de mascoco
    Profil pro
    chef
    Inscrit en
    Octobre 2006
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : chef

    Informations forums :
    Inscription : Octobre 2006
    Messages : 66
    Par défaut Binding entre 2 contrôles WPF
    Salut,

    Je cherche comment faire un Bind entre 2 fenêtres, je m'explique : j'ai crée un contrôle personnalisable, ademettant son nom myControlPerso c'est une rotateur qu'on tourne avec la souris et il nous donne la valeur d'un angle.

    puis dans une fenêtre j'instancie un objet de type myControlPerso qu'on peut manipuler, et j'ai un lable qui doit afficher la valeur de l'angle courant.

    comment faire pour binder ce label avec l'objet myControlPerso et récupérer l'angle courant qui est un champs de type double dans la classe du myControlPerso

    j'ai mon XMAL

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
        <Grid Background="{StaticResource WindowBackground}" >
            <local:Dial x:Name="Dial01" Loaded="Dial01_Loaded"></local:Dial>
     
            <TextBlock Background="Black" Foreground="White" Text="{Binding ElementName=Dial01, Path=monAngle}"  Height="21" HorizontalAlignment="Left" Margin="185,0,0,180" Name="textBlock1" VerticalAlignment="Bottom" Width="120" />
     
        </Grid>
    myControlPerso correspond à <localial x:Name="Dial01"...

    et dans ma classe (code behind) myControlPerso j'ai un champs de type Double appelé "monAngle", il est public et se met à jour dans une méthode qui gère l'évenement de la rotation pour récupérer l'angle courant.
    merci

  2. #2
    Membre actif Avatar de mascoco
    Profil pro
    chef
    Inscrit en
    Octobre 2006
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : chef

    Informations forums :
    Inscription : Octobre 2006
    Messages : 66
    Par défaut
    bon j'ai réussi en ajoutant les get et set mais le problème au lancement je récupère la valeur dans le champs de texte mais quand je change l'angle en tournant le cercle la champs ne se met pas à jour automatiquement !!!

    comment renseigner ça dans XMAL afin qu'il se mette à jour ?

    merci

  3. #3
    Membre Expert
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    1 103
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 1 103
    Par défaut
    la propriété Angle exposée par ton UserControl doit avoir un mécanisme de suivit.

    Ainsi ton UserControl doit implémenter l'interface INotifyPropertyChanged et notifier de tout changement d'état de sa propriété Angle, si c'est bien sur le UserControl que tu expose cette propriété.

    Si tu l'expose ailleurs, il faudrait quelques passages de ton code.

    L'autre solution consisterait à définir la propriété Angle comme une propriété de dépendance sur ton UserControl. Elle deviendrait de fait candidate au notifications de suivit, mais je ne suis pas sure que ce soit nécessaire dans ton cas.

    Tu ne peux pas dire à XAML de recharger l'état d'une propriété s'il pense que celle ci n'a pas subit de changement, et quelque soit le mode de binding que tu applique

  4. #4
    Membre actif Avatar de mascoco
    Profil pro
    chef
    Inscrit en
    Octobre 2006
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : chef

    Informations forums :
    Inscription : Octobre 2006
    Messages : 66
    Par défaut
    Citation Envoyé par cinemania Voir le message
    la propriété Angle exposée par ton UserControl doit avoir un mécanisme de suivit.

    Ainsi ton UserControl doit implémenter l'interface INotifyPropertyChanged et notifier de tout changement d'état de sa propriété Angle, si c'est bien sur le UserControl que tu expose cette propriété.

    Si tu l'expose ailleurs, il faudrait quelques passages de ton code.

    L'autre solution consisterait à définir la propriété Angle comme une propriété de dépendance sur ton UserControl. Elle deviendrait de fait candidate au notifications de suivit, mais je ne suis pas sure que ce soit nécessaire dans ton cas.

    Tu ne peux pas dire à XAML de recharger l'état d'une propriété s'il pense que celle ci n'a pas subit de changement, et quelque soit le mode de binding que tu applique

    j'ai pas réussi

    j'ai suivi cette exemple : http://msdn.microsoft.com/en-us/libr...tychanged.aspx

    voilà la classe :

    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
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
     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;
    using Microsoft.Surface;
    using Microsoft.Surface.Presentation;
    using Microsoft.Surface.Presentation.Controls;
    using Microsoft.Surface.Presentation.Manipulations;
    using System.ComponentModel;
     
    namespace TouchEnabledUserControl
    {
        /// <summary>
        /// Interaction logic for Dial.xaml
        /// </summary>
        public partial class Dial  : SurfaceUserControl,  INotifyPropertyChanged 
        {
     
            /// <summary>
            /// Declare manipulation processor.
            /// </summary>
            private Affine2DManipulationProcessor manipulationProcessor;
            public Binding bind;
     
            private double _monAngle;
            public double monAngle
            {
                get
                {
                    return _monAngle;
                }
                set 
                {
                    _monAngle = value;
    //                NotifyPropertyChanged("AngleChanged");
     
                }
            }
     
            public event PropertyChangedEventHandler PropertyChanged;
            private void NotifyPropertyChanged(String info)
            {
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs(info));
                }
            }
     
     
            /// <summary>
            /// Constructor for Dial component. Initializes manipulation processor.
            /// </summary>
            public Dial()
            {
                InitializeComponent();
                InitializeManipulationProcessor();
                //bind = new Binding();
                //bind.Source = DialRotateTransform.Angle;
                //bind.Source = "4";
                //bind.Mode = BindingMode.OneWay;
                _monAngle = 0;
            }
     
            /// <summary>
            /// Initializes the manipulation processor to accept rotation and a pivot point.
            /// Handles only changes in manipulations, not manipulation start or stop events.
            /// </summary>
            private void InitializeManipulationProcessor()
            {
                manipulationProcessor = new Affine2DManipulationProcessor(Affine2DManipulations.Rotate, DialGrid, new Point(125, 125));
                manipulationProcessor.Affine2DManipulationDelta += OnManipulationDelta;
     
            }
     
     
     
            #region ManipulationDelta
     
            /// <summary>
            /// Handles changes in manipulation.
            /// </summary>
            /// <param name="sender">Object</param>
            /// <param name="e">Event argument</param>
            private void OnManipulationDelta(object sender, Affine2DOperationDeltaEventArgs e)
            {
                DialRotateTransform.Angle += e.RotationDelta;
                _monAngle = DialRotateTransform.Angle;
                //_monAngle = 100;
                NotifyPropertyChanged("AngleChanged");
                Console.WriteLine("in OnManipulationDelta Angle = "+_monAngle);
     
                /*if (bind == null)
                {
                    bind = new Binding(){ Source = DialRotateTransform.Angle, Mode = BindingMode.OneWay };
                }*/
     
                //surfaceTextBox1.Text = Convert.ToString(DialRotateTransform.Angle);
                if((DialRotateTransform.Angle > 35) & (DialRotateTransform.Angle < 50)){
                    //SurfaceWindow1.surfaceTextBoxFirstPlan.Text = "premiere zone";
                    surfaceTextBox1.Text = "premiere zone";
                }
                else if ((DialRotateTransform.Angle > 80) & (DialRotateTransform.Angle < 100))            
                {
                    surfaceTextBox1.Text = "deuxième zone";
                    //SurfaceWindow1.surfaceTextBoxFirstPlan.Text = "deuxième zone";
                }
                else if ((DialRotateTransform.Angle > 125) & (DialRotateTransform.Angle < 145))
                {
                    //SurfaceWindow1.surfaceTextBoxFirstPlan.Text = "troisième zone";
                    surfaceTextBox1.Text = "troisième zone";
                }
                else
                {
                    surfaceTextBox1.Text = "";
                }
            }
     
            #endregion
     
            #region CaptureContact
            /// <summary>
            /// Captures contact and starts the manipulation processor.
            /// </summary>
            /// <param name="e">Contact event.</param>
            protected override void OnContactDown(ContactEventArgs e)
            {
                base.OnContactDown(e);
     
                // Capture this contact
                e.Contact.Capture(this);           
                manipulationProcessor.BeginTrack(e.Contact);
     
                // Mark this event as handled
                e.Handled = true;
            }
            #endregion
     
     
     
     
        }
    }

    XAML de la fenetre qui utilise ce control perso:
    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
     
    <s:SurfaceWindow x:Class="TouchEnabledUserControl.SurfaceWindow1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="http://schemas.microsoft.com/surface/2008"
        xmlns:local="clr-namespace:TouchEnabledUserControl"
        Title="TouchEnabledUserControl"
        >
        <s:SurfaceWindow.Resources>
            <ImageBrush x:Key="WindowBackground" Stretch="None" Opacity="0.6" ImageSource="pack://application:,,,/Resources/WindowBackground.jpg"/>
        </s:SurfaceWindow.Resources>
     
        <Grid Background="{StaticResource WindowBackground}" >
            <local:Dial x:Name="Dial01" Loaded="Dial01_Loaded"></local:Dial>
            <s:SurfaceTextBox  Text="" Height="23" HorizontalAlignment="Right" Margin="0,0,19,70" Name="surfaceTextBoxFirstPlan" VerticalAlignment="Bottom" Width="120"/>
            <TextBlock x:Name="monTextBlock" Background="Black" Foreground="White" Text="{Binding ElementName=Dial01, Path=monAngle}"   Height="21" HorizontalAlignment="Left" Margin="185,0,0,180" Name="textBlock1" VerticalAlignment="Bottom" Width="120" />
     
     
            </Grid>
     
     
    </s:SurfaceWindow>

    XAML du control :

    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
    <s:SurfaceUserControl x:Class="TouchEnabledUserControl.Dial"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:s="http://schemas.microsoft.com/surface/2008">
        <Grid Height="300" Width="300">
            <Grid Name="DialGrid" Margin="25,25,124.236,117.81">
                <Grid.RenderTransformOrigin>
                    <Point X="0.5" Y="0.5"/>
                </Grid.RenderTransformOrigin>
     
                <Grid.RenderTransform>
                    <RotateTransform x:Name="DialRotateTransform" Angle="0" CenterX="0" CenterY="0"/>
                </Grid.RenderTransform>            
                    <Ellipse Name="ellipse1" Stroke="Gray" StrokeThickness="2" />            
                <Grid>
                    <Rectangle Margin="67.5,1.5,71,0" Name="First" Stroke="Gray" Fill="White" Height="41.5" VerticalAlignment="Top" StrokeThickness="3"/>
                    <Rectangle Margin="67.5,42.5,71,1.25" Name="Secound" Stroke="Gray" Fill="Black" StrokeThickness="3" />
                </Grid>
            </Grid>
            <s:SurfaceTextBox Height="23" HorizontalAlignment="Right" Margin="0,31,0,0" Name="surfaceTextBox1" VerticalAlignment="Top" Width="120" />
        </Grid>
     
    </s:SurfaceUserControl>

    pour info le champs de texte "surfaceTextBox1" qui est intégré dans la classe du control pero affiche bien les textes quand tu tourne avec la main l'ellipse, le problème c'est uniquement la mise à jour de monTextBlock qui est dans la fenetre SurfaceWindow1

    j'attends ta réponse, merci

  5. #5
    Membre Expert
    Profil pro
    Inscrit en
    Juillet 2006
    Messages
    1 103
    Détails du profil
    Informations personnelles :
    Âge : 47
    Localisation : France, Meurthe et Moselle (Lorraine)

    Informations forums :
    Inscription : Juillet 2006
    Messages : 1 103
    Par défaut
    c'est presque ca, mais quand tu fait

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    NotifyPropertyChanged("AngleChanged");
    il faut le remplacer par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    NotifyPropertyChanged("monAngle");
    Dans le code de ton SurfaceUserControl Dial.

  6. #6
    Membre actif Avatar de mascoco
    Profil pro
    chef
    Inscrit en
    Octobre 2006
    Messages
    66
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : chef

    Informations forums :
    Inscription : Octobre 2006
    Messages : 66
    Par défaut
    Citation Envoyé par cinemania Voir le message
    c'est presque ca, mais quand tu fait

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    NotifyPropertyChanged("AngleChanged");
    il faut le remplacer par

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    NotifyPropertyChanged("monAngle");
    Dans le code de ton SurfaceUserControl Dial.
    Merci beaucoup, ça marche enfin

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

Discussions similaires

  1. Réponses: 7
    Dernier message: 11/05/2015, 15h13
  2. [WPF]Références partagé entre un Contrôle et une liste
    Par Babas007 dans le forum Windows Presentation Foundation
    Réponses: 18
    Dernier message: 14/11/2010, 19h00
  3. binding entre un objet wpf et un objet COM
    Par TERRIBLE dans le forum Windows Presentation Foundation
    Réponses: 4
    Dernier message: 07/05/2010, 15h58
  4. Binding TwoWay entre deux contrôles
    Par Lelio1407 dans le forum Windows Presentation Foundation
    Réponses: 1
    Dernier message: 28/10/2009, 18h01
  5. [WPF] Problème de binding entre une SortedList et une ListBox
    Par Invité dans le forum Général Dotnet
    Réponses: 4
    Dernier message: 10/12/2007, 13h20

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