2014-03-29 32 views
1

我正在引用http://msdn.microsoft.com/en-us/library/cc645061(v=vs.95).aspx以便在按下時更改文本塊文本的前景,但我在<Setter Property="Template">行發生錯誤,指出The member 'Template' is not recognized or is not accessible。我想,默認情況下,將前景設置爲設備的PhoneAccentBrush,然後按下時將前景設置爲PhoneDisabledBrush(灰色)。我如何在WP8中完成這項工作?當按下時更改TextBlock前景

<Style x:Key="TextBlockStyle1" TargetType="TextBlock"> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeNormal}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Margin" Value="{StaticResource PhoneHorizontalMargin}"/> 
     <Setter Property="Template"> <!-- Error: The member 'Template' is not recognized or is not accessible. --> 
      <Setter.Value> 
       <ControlTemplate TargetType="TextBox"> 
        <Grid x:Name="RootElement"> 
         <vsm:VisualStateManager.VisualStateGroups> 
          <vsm:VisualStateGroup x:Name="CommonStates"> 
           <vsm:VisualState x:Name="Normal"/> 
           <vsm:VisualState x:Name="MouseOver"> 
            <Storyboard> 
             <ColorAnimation Storyboard.TargetName="MouseOverBorder" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" To="#FF99C1E2" Duration="0"/> 
            </Storyboard> 
           </vsm:VisualState> 
           <vsm:VisualState x:Name="Disabled"> 
            <Storyboard> 
             <DoubleAnimation Storyboard.TargetName="DisabledVisualElement" Storyboard.TargetProperty="Opacity" To="1" Duration="0"/> 
            </Storyboard> 
           </vsm:VisualState> 
          </vsm:VisualStateGroup> 
         </vsm:VisualStateManager.VisualStateGroups> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 

    </Style> 

回答

1

TextBlock沒有Template屬性。解決您的問題可以製作Button並將其自定義爲TextBlock。

下面是一個例子:

 <Button Content="Test" 
       Foreground="{StaticResource PhoneAccentBrush}"> 
      <Button.Style> 
       <Style TargetType="Button"> 
        <Setter Property="Template"> 
         <Setter.Value> 
          <ControlTemplate TargetType="Button"> 
           <Grid Background="Transparent"> 
            <VisualStateManager.VisualStateGroups> 
             <VisualStateGroup x:Name="CommonStates"> 
              <VisualState x:Name="Pressed"> 
               <Storyboard> 
                <ObjectAnimationUsingKeyFrames 
                 Storyboard.TargetProperty="Foreground" 
                 Storyboard.TargetName="Txt"> 
                 <DiscreteObjectKeyFrame KeyTime="0" 
                       Value="{StaticResource PhoneDisabledBrush}" /> 
                </ObjectAnimationUsingKeyFrames> 
               </Storyboard> 
              </VisualState> 
             </VisualStateGroup> 
            </VisualStateManager.VisualStateGroups> 

            <TextBlock x:Name="Txt" 
               Foreground="{TemplateBinding Foreground}" 
               Text="{TemplateBinding Content}" /> 

           </Grid> 
          </ControlTemplate> 
         </Setter.Value> 
        </Setter> 
       </Style> 
      </Button.Style> 
     </Button>