2013-05-08 5 views
1

Les blocs de texte peuvent-ils être mis en focus dans WPF? Je veux changer la couleur d'arrière-plan du bloc de texte si c'est actuellement le focus, mais je veux le faire en XAML. C'est ce que j'ai maintenant. C'est un tas de textboxes dans un Stackpanel. Je peux obtenir le XAML pour cibler le non-focus ou l'état de base, mais lorsque j'essaie d'ajouter un trigger, l'arrière-plan ne change pas de focus. Le code est ci-dessous:XAML/WPF focus textblock

<Style x:Key="QueueListTextBlocks" TargetType="TextBlock"> 
      <Setter Property="Background" Value="#027802"></Setter> 
      <Setter Property="Foreground" Value="White"></Setter> 
      <Setter Property="Padding" Value="10,5"></Setter> 
      <Setter Property="Margin" Value="5,2,5,0"></Setter> 
      <Setter Property="FontSize" Value="14"></Setter> 
      <Setter Property="Focusable" Value="true"/> 
      <Setter Property="Cursor" Value="Hand"></Setter> 
      <!-- Trigger--> 
      <Style.Triggers> 
<!--Does not pick up a IsFucused State--Alternative?--> 

       <Trigger Property="IsFocused" Value="True"> 
        <Setter Property="Background" Value="Blue"></Setter> 
        <Setter Property="FontSize" Value="18"></Setter> 
        <Setter Property="Foreground" Value="Orange"></Setter> 
       </Trigger> 

      </Style.Triggers> 
      <!--<Setter Property="Background" Value="White" />--> 
     </Style> 
+0

http://stackoverflow.com/questions/3351519/focus-on-label-textblock-and-border. S'il vous plaît regardez cette réponse. – Gilad

+0

Lorsque je fais cela, il indique que la propriété = "Modèle" n'est pas reconnu ou accessible – ClosDesign

Répondre

0

J'ai essayé votre style et cela fonctionne parfaitement. Les TextBlocks dans ma fenêtre changent leur apparence en appuyant simplement sur la touche TAB. J'utilise le framework .NET 4.0.

C'est le XAML de ma fenêtre:

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 

    <Window.Resources> 
     <Style TargetType="TextBlock" x:Key="TextBlockStyle"> 
      <Setter Property="Background" Value="#027802"></Setter> 
      <Setter Property="Foreground" Value="White"></Setter> 
      <Setter Property="Padding" Value="10,5"></Setter> 
      <Setter Property="Margin" Value="5,2,5,0"></Setter> 
      <Setter Property="FontSize" Value="14"></Setter> 
      <Setter Property="Focusable" Value="true"/> 
      <Setter Property="Cursor" Value="Hand"></Setter> 
      <!-- Trigger--> 
      <Style.Triggers> 
       <!--Does not pick up a IsFucused State-Alternative?--> 

       <Trigger Property="IsFocused" Value="True"> 
        <Setter Property="Background" Value="Blue"></Setter> 
        <Setter Property="FontSize" Value="18"></Setter> 
        <Setter Property="Foreground" Value="Orange"></Setter> 
       </Trigger> 

      </Style.Triggers> 
      <!--<Setter Property="Background" Value="White" />--> 
     </Style> 
    </Window.Resources> 

    <StackPanel Orientation="Vertical"> 
     <TextBlock Text="One" Style="{StaticResource TextBlockStyle}" /> 
     <TextBlock Text="Two" Style="{StaticResource TextBlockStyle}" /> 
     <TextBlock Text="Three" Style="{StaticResource TextBlockStyle}" /> 
     <TextBlock Text="Four" Style="{StaticResource TextBlockStyle}" /> 
     <TextBlock Text="Five" Style="{StaticResource TextBlockStyle}" /> 
    </StackPanel> 
</Window> 

J'espère que cela aide