2014-03-29 29 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>