2015-11-20 2 views
3

Dans mon application universelle plate-forme Windows, j'ai un TextBlock dont DataContext est une classe personnalisée:ChangePropertyAction n'affecte pas FontWeight

<TextBlock Text="{Binding OpeningHourDescription}" FontWeight="Light"> 
    ... 
</TextBlock> 

Ma classe personnalisée possède une propriété IsOpen. Si c'est vrai, le TextBlock devrait changer sa couleur et son poids. J'ai essayé de définir ce en remplaçant le code XAML suivant dans l'élément TextBlock:

<Interactivity:Interaction.Behaviors> 
    <Core:DataTriggerBehavior Binding="{Binding IsOpen}" Value="true"> 
     <Core:ChangePropertyAction PropertyName="Foreground" > 
      <Core:ChangePropertyAction.Value> 
       <Color>Lime</Color> 
      </Core:ChangePropertyAction.Value> 
     </Core:ChangePropertyAction> 
     <Core:ChangePropertyAction PropertyName="FontWeight"> 
      <Core:ChangePropertyAction.Value> 
       <FontWeight>ExtraBold</FontWeight> 
      </Core:ChangePropertyAction.Value> 
     </Core:ChangePropertyAction> 
    </Core:DataTriggerBehavior> 
</Interactivity:Interaction.Behaviors> 

Le comportement lui-même fonctionne: Si IsOpen est vrai, le texte est vert. Toutefois, le FontWeight n'est pas défini, le texte n'a jamais l'air gras. Curieusement, l'échange des deux actions ChangePropertyAction entraîne l'application des deux actions.

Modifier FontWeight statiquement sur le TextBlock fonctionne comme prévu. Le problème se produit également lorsque FontWeight="Light" est pas défini sur le TextBlock.

Qu'est-ce que je fais mal? Comment puis-je modifier le FontWeight avec un ChangePropertyAction?

+1

Si je me souviens bien, vous pouvez le faire avec un storyboard et ObjectAnimationUsingKeyFrames –

Répondre

2

Si vous regardez dans l'API Windows, vous verrez qu'une propriété TextBlock.FontWeight est de type FontWeight, qui est une structure contenant un Int16.

public struct FontWeight 
{ 
    public System.UInt16 Weight; 
} 

Si vous définissez la propriété XAML sur le contrôle, il parvient à convertir la chaîne en une propriété statique FontWeights.

public sealed class FontWeights : IFontWeights 
{ 
    public static FontWeight Black { get; } 
    public static FontWeight Bold { get; } 
    public static FontWeight ExtraBlack { get; } 
    public static FontWeight ExtraBold { get; } 
    public static FontWeight ExtraLight { get; } 
    public static FontWeight Light { get; } 
    public static FontWeight Medium { get; } 
    public static FontWeight Normal { get; } 
    public static FontWeight SemiBold { get; } 
    public static FontWeight SemiLight { get; } 
    public static FontWeight Thin { get; } 
} 

Cependant, dans la gâchette vous déclarez que la chaîne vous entrez est en fait une struct FontWeight (et non une propriété FontWeights), ainsi le système a une erreur en essayant de convertir la chaîne.

<Core:ChangePropertyAction.Value> 
    <FontWeight>ExtraBold</FontWeight> 
</Core:ChangePropertyAction.Value> 

Solution: Vous pouvez utiliser un GoToStateAction et VisualStates pour résoudre votre problème.

<TextBlock x:Name="MyTextBlock" Text="{Binding OpeningHourDescription}" FontWeight="Light"> 
    <interactivity:Interaction.Behaviors> 
     <core:DataTriggerBehavior Binding="{Binding IsOpen}" Value="true"> 
      <core:GoToStateAction StateName="Open" > 
      </core:GoToStateAction> 
     </core:DataTriggerBehavior> 
    </interactivity:Interaction.Behaviors> 
</TextBlock> 

Ajoutez les états corrects à votre contrôle de conteneur.

<VisualStateManager.VisualStateGroups> 
    <VisualStateGroup x:Name="VisualStateGroup"> 
     <VisualState x:Name="Open"> 
      <VisualState.Setters> 
       <Setter Target="MyTextBlock.Foreground" Value="Lime" /> 
       <Setter Target="MyTextBlock.FontWeight" Value="Bold" /> 
      </VisualState.Setters> 
     </VisualState> 
     ... 
    </VisualStateGroup> 
</VisualStateManager.VisualStateGroups> 
+0

En UWP vous pouvez utiliser l'état visuel [déclencheur] (https://msdn.microsoft.com/library/windows/apps/windows. ui.xaml.visualstate.statetriggers.aspx). –

+0

On dirait que je ne peux pas l'utiliser puisque mon 'TextBlock' est en fait un template pour un' ItemsControl', donc j'ai plusieurs copies avec des états indépendants. Est-ce que je regarde un chemin? – fefrei

+0

@fefrei Dans ce cas vous avez raison. Dans 'DataTemplate', vous ne pouvez pas utiliser' StateTrigger'. –