Search code examples
xamlwindows-phone-8textblock

Change TextBlock Foreground when Pressed


I was referencing http://msdn.microsoft.com/en-us/library/cc645061(v=vs.95).aspx in order to change the foreground of the textblock text when it is pressed, but I am getting an error on line <Setter Property="Template"> stating that The member 'Template' is not recognized or is not accessible. I would like to, by default, set the foreground to the device's PhoneAccentBrush and then when pressed set the foreground to PhoneDisabledBrush (the grayish color). How might I accomplish this in WP8?

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

Solution

  • TextBlock has no Template property. As work around you can make a Button and customize it as TextBlock.

    Here is an example:

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