2013-05-08 94 views
1

文本塊是否可在WPF中聚焦?我想改變文本塊的背景顏色,如果它目前是一個焦點,但我想在XAML中做。 這就是我現在擁有的。這是一堆Stackpanel中的文本框。我可以讓XAML定位到非焦點或基本狀態,但是當我嘗試添加觸發器時,背景在焦點上不會改變。代碼如下:XAML/WPF焦點文本塊

<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。請看這個答案。 – Gilad 2013-05-08 20:21:47

+0

當我這樣做時,它表明Property =「Template」不被識別或訪問 – ClosDesign 2013-08-16 15:18:00

回答

0

我試過你的風格,它的功能完美。 我的窗口中的TextBlocks只是按下TAB鍵就可以改變它們的外觀。 我正在使用.NET 4.0框架。

這是我窗口的XAML:

<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> 

我希望它能幫助