按下时更改TextBlock前景

时间:2014-03-29 18:10:48

标签: xaml windows-phone-8 textblock

我正在引用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中实现这一目标?

<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 个答案:

答案 0 :(得分: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>