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:
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
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; } }
Le fait de passer la DP à true ou false rend le control flag visible ou collapsed
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)); }
À l'initialisation, je crée un PlayerInformation avec PlayerArmyColor = Blue :
Cela devrait donc rendre collapsed tous les drapeaux à l'exception du Bleu.
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>
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 :
En tout cas, depuis OnPlayorArmyColorChanged de la DP PlayerArmyColor ...
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)); }
Car lorsque je modifie directement IsRedFlagOwnedByPlayer cela fonctionne bien ...
Pour compléter l'information, le code XAML du PlayerInformation:
Merci d'avance pour votre aide.
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>
Partager