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

Silverlight Discussion :

Modification d'une Dependency Property dans une autre DP


Sujet :

Silverlight

  1. #1
    Membre à l'essai
    Inscrit en
    Décembre 2009
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Décembre 2009
    Messages : 16
    Points : 13
    Points
    13
    Par défaut Modification d'une Dependency Property dans une autre DP
    Bonjour,

    Dans un UserControl PlayerInformation, j'ai plusieurs DP:

    PlayerArmyColor
    IsRedFlagOwnedByPlayer
    IsBlueFlagOwnedByPlayer
    IsYellowFlagOwnedByPlayer
    IsGreenFlagOwnedByPlayer

    Chaque Flag est un UserControl que je rends visible ou pas si le joueur possède ou non le drapeau.

    À l'initialisation, le joueur ne possède que le drapeau de sa couleur.

    J'ai donc le code suivant:

    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
     
     public ArmyColor PlayerArmyColor
            {
                get { return (ArmyColor)GetValue(PlayerArmyColorProperty); }
                set { SetValue(PlayerArmyColorProperty, value); }
            }
     
            public static readonly DependencyProperty PlayerArmyColorProperty =
                DependencyProperty.Register("PlayerArmyColor", typeof(ArmyColor), typeof(PlayerInformation), new PropertyMetadata(OnPlayerArmyColorChanged));
     
            private static void OnPlayerArmyColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var ctrl = (PlayerInformation)d;
                var ac = (ArmyColor)e.NewValue;
                switch (ac)
                {
                    case ArmyColor.Blue :
                        ctrl.IsBlueFlagOwnedByPlayer = true;
                        ctrl.IsRedFlagOwnedByPlayer = false;
                        ctrl.IsGreenFlagOwnedByPlayer = false;
                        ctrl.IsYellowFlagOwnedByPlayer = false;
                        break;
                    case ArmyColor.Red:
                        ctrl.IsBlueFlagOwnedByPlayer = false;
                        ctrl.IsRedFlagOwnedByPlayer = true;
                        ctrl.IsGreenFlagOwnedByPlayer = false;
                        ctrl.IsYellowFlagOwnedByPlayer = false;
                        break;
                    case ArmyColor.Green:
                        ctrl.IsBlueFlagOwnedByPlayer = false;
                        ctrl.IsRedFlagOwnedByPlayer = false;
                        ctrl.IsGreenFlagOwnedByPlayer = true;
                        ctrl.IsYellowFlagOwnedByPlayer = false;
                        break;
                    case ArmyColor.Yellow:
                        ctrl.IsBlueFlagOwnedByPlayer = false;
                        ctrl.IsRedFlagOwnedByPlayer = false;
                        ctrl.IsGreenFlagOwnedByPlayer = false;
                        ctrl.IsYellowFlagOwnedByPlayer = true;
                        break;
                }
            }
    En fonction de la couleur j'attribue donc une valeur aux autres DP qui sont toutes de la forme suivante:

    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
     
      public bool IsYellowFlagOwnedByPlayer
            {
                get { return (bool)GetValue(IsYellowFlagOwnedByPlayerProperty); }
                set { SetValue(IsYellowFlagOwnedByPlayerProperty, value); }
            }
     
            public static readonly DependencyProperty IsYellowFlagOwnedByPlayerProperty =
                DependencyProperty.Register("IsYellowFlagOwnedByPlayer", typeof(bool), typeof(PlayerInformation), new PropertyMetadata(false, OnIsYellowFlagOwnedChanged));
     
            public static void OnIsYellowFlagOwnedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                var ctrl = d as PlayerInformation;
                bool isTrue = (bool)e.NewValue;
                ctrl.YellowFlag.Visibility = (Visibility)(new VisibilityConverter().Convert(isTrue, typeof(Visibility), null, null));
            }
    Le fait de passer la DP à true ou false rend le control flag visible ou collapsed

    À l'initialisation, je crée un PlayerInformation avec PlayerArmyColor = Blue :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <Canvas x:Name="LayoutRoot" Background="White">
    		<local:PlayerInformation x:Name="playerInformation" Canvas.Left="19" Canvas.Top="17" d:LayoutOverrides="Width, Height" PlayerName="Pierre" MineralsNumber="123" PlayerArmyColor="Blue"/>
    		<TextBlock x:Name="TbRed" Height="12" Canvas.Left="60" TextWrapping="Wrap" Text="{Binding IsRedFlagOwnedByPlayer, ElementName=playerInformation}" Canvas.Top="83" Width="84"/>
    		<TextBlock x:Name="TbBlue" Height="12" Canvas.Left="60" TextWrapping="Wrap" Text="{Binding IsBlueFlagOwnedByPlayer, ElementName=playerInformation}" Canvas.Top="99" Width="84"/>
    		<TextBlock x:Name="TbGreen" Height="12" Canvas.Left="60" TextWrapping="Wrap" Text="{Binding IsGreenFlagOwnedByPlayer, ElementName=playerInformation}" Canvas.Top="67" Width="84"/>
    		<TextBlock x:Name="TbYellow" Height="12" Canvas.Left="60" TextWrapping="Wrap" Text="{Binding IsYellowFlagOwnedByPlayer, ElementName=playerInformation}" Canvas.Top="115" Width="84"/>
    		<TextBlock Height="12" Canvas.Left="8" TextWrapping="Wrap" Text="Blue" Canvas.Top="99" Width="48"/>
    		<TextBlock Height="12" Canvas.Left="8" TextWrapping="Wrap" Text="Green" Canvas.Top="67" Width="48"/>
    		<TextBlock Height="12" Canvas.Left="8" TextWrapping="Wrap" Text="Red" Canvas.Top="83" Width="48"/>
    		<TextBlock Height="12" Canvas.Left="8" TextWrapping="Wrap" Canvas.Top="115" Width="48"><Run Text="Yellow"/><LineBreak/><Run/></TextBlock>
    	</Canvas>
    Cela devrait donc rendre collapsed tous les drapeaux à l'exception du Bleu.

    Au debuggage il passe bien là où il faut :


    Mais dans Blend tout comme à l'éxécution tous les drapeaux restent visibles et pourtant les valeurs lues sont bonnes (à false), mais les collapsed n'ont semble-t-il pas été appelés ?



    En fait, il semblerait que de modifier une DP au sein d'une DP ne déclenche ensuite pas tout correctement ? Par exemple, passer le IsRedFlagOwnedByPlayer à false, aurait du appeler :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
     public static void OnIsRedFlagOwnedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    		{
                var ctrl = d as PlayerInformation;
                bool isTrue = (bool)e.NewValue;
                ctrl.RedFlag.Visibility = (Visibility)(new VisibilityConverter().Convert(isTrue, typeof(Visibility), null, null));
    		}
    En tout cas, depuis OnPlayorArmyColorChanged de la DP PlayerArmyColor ...
    Car lorsque je modifie directement IsRedFlagOwnedByPlayer cela fonctionne bien ...

    Pour compléter l'information, le code XAML du PlayerInformation:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    <Canvas x:Name="LayoutRoot" Background="Black">
        	<TextBlock x:Name="TxtBlPlayerName" Text="{Binding PlayerName}" Foreground="{Binding PlayerArmyColor, Converter={StaticResource ArmyColorToArmyDarkBrush}}" FontSize="16" Width="181" Height="20" ToolTipService.ToolTip="{Binding PlayerInformationNameDescription, Source={StaticResource Lang}}" Canvas.Left="5" />
        	<controlsToolkit:WrapPanel x:Name="wp" Height="18" Canvas.Left="8" Canvas.Top="21" Width="50" ToolTipService.ToolTip="{Binding PlayerInformationFlagsDescription, Source={StaticResource Lang}}" Margin="0">
                <local:ArmyFlag x:Name="RedFlag" HorizontalAlignment="Right" Width="auto" Margin="0,0,2,0" VerticalAlignment="Bottom" FlagColor="Red" />
                <local:ArmyFlag x:Name="GreenFlag" HorizontalAlignment="Right" Width="auto" Margin="0,0,2,0" VerticalAlignment="Bottom" FlagColor="Green" />
        		<local:ArmyFlag x:Name="BlueFlag" HorizontalAlignment="Right" Width="auto" Margin="0,0,2,0" VerticalAlignment="Bottom" FlagColor="Blue" />
        		<local:ArmyFlag x:Name="YellowFlag" HorizontalAlignment="Right" Width="auto" Margin="0,0,2,0" VerticalAlignment="Bottom" FlagColor="Yellow" />
        	</controlsToolkit:WrapPanel>
        	<local:Mineral x:Name="MineralsInformation" MineralsNumber="{Binding MineralsNumber}" MinHeight="29" Canvas.Left="186" Canvas.Top="6" Width="34" Margin="0" Height="21" />
        	<local:Grade Canvas.Left="82" Canvas.Top="19" d:LayoutOverrides="Width, Height"/>
        </Canvas>
    Merci d'avance pour votre aide.

  2. #2
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Je viens de tester de modifier une DP dans une DP et je passe bien dans le Changed.

    Au debug si tu mets des breakpoints dans les Changed de toutes tes DP est-ce que tu passes dans chaque ?

    A la place d'instancier une VisibilityConverter tu peux mettre tout simplement ça

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ctrl.RedFlag.Visibility = isTrue ? Visibility.Visible : Visibility.Collapsed;

  3. #3
    Membre à l'essai
    Inscrit en
    Décembre 2009
    Messages
    16
    Détails du profil
    Informations forums :
    Inscription : Décembre 2009
    Messages : 16
    Points : 13
    Points
    13
    Par défaut
    En fait il passe dans les OnxxxxxChanged lorsque que je les set à True uniquement!

    Donc j'imagine que par défaut la valeur est false .... je set à false cela ne déclenche pas le changed et donc ne collapse pas mes contrôles ....

    Autrement dit, il faut que j'initialise les états visuels en dehors de cette évènement ..?

    Merci pour la syntaxe.

  4. #4
    Expert éminent sénior
    Avatar de Skyounet
    Homme Profil pro
    Software Engineer
    Inscrit en
    Mars 2005
    Messages
    6 380
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : Etats-Unis

    Informations professionnelles :
    Activité : Software Engineer
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mars 2005
    Messages : 6 380
    Points : 13 380
    Points
    13 380
    Par défaut
    Ah ben oui par défaut un bool vaut false, donc si tu le re-set à false derrière ça va pas appeler le OnChanged.

    Ce que tu peux faire c'est qu'au chargement de ton UC, mettre tes 4 DP à false, ou bien cacher les 4 drapeaux (Visibility à Collapsed) par défaut aussi.

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 22/03/2010, 15h40
  2. Réponses: 6
    Dernier message: 13/11/2009, 16h06
  3. Binding d'une dependency property vers une autre
    Par Pragmateek dans le forum Windows Presentation Foundation
    Réponses: 8
    Dernier message: 31/07/2009, 17h28
  4. Réponses: 2
    Dernier message: 12/01/2009, 15h33
  5. Réponses: 1
    Dernier message: 11/06/2008, 13h33

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