【问题标题】:How to change TextBlock foreground color on text changed for Windows Phone 8.1 (WinRT)?如何更改为 Windows Phone 8.1 (WinRT) 更改的文本的 TextBlock 前景色?
【发布时间】:2015-03-22 02:44:02
【问题描述】:

我的 TextBlock 已绑定到我的视图模型,我想在文本更改时闪烁文本。我发现很难为 Windows Phone 8.1 (WinRT) 实现这一点。我想我必须使用 EventTriggerBehavior 并将 textBlock 更改为 textBox 然后选择“TextChanged”事件。有没有简单的方法可以做到这一点?

这是我尝试使用 TextBox 并使用 EventTriggerBehavior。

<TextBlock x:Name="TestTypeTextBox"
           TextWrapping="Wrap" 
           Text="{Binding TestTypeText}" 
           FontSize="48" TextAlignment="Center" 
           HorizontalAlignment="Center" 
           VerticalAlignment="Center" 
           Margin="0" 
           FontWeight="Bold" 
           FontFamily="Segoe UI Black"
           Foreground="White" 
           Padding="0">
    <i:Interaction.Behaviors>
        <core:EventTriggerBehavior EventName="SelectionChanged">
            <Media:ControlStoryboardAction Storyboard="{StaticResource FlashingText}"/>
        </core:EventTriggerBehavior>
    </i:Interaction.Behaviors>
</TextBlock>

【问题讨论】:

    标签: windows xaml windows-runtime windows-phone-8.1 expression-blend


    【解决方案1】:

    鉴于您已经以 mvvm 方式执行此操作,而不是依赖事件来调用情节提要,如何监控 TestTypeText 的属性变化?

    这样做,您将需要DataTriggerBehavior 而不是EventTriggerBehavior

    <TextBlock x:Name="TestTypeTextBox"
               TextWrapping="Wrap" 
               Text="{Binding TestTypeText,FallbackValue=sss}" 
               FontSize="48" TextAlignment="Center" 
               HorizontalAlignment="Center" 
               VerticalAlignment="Center" 
               Margin="0" 
               FontWeight="Bold" 
               FontFamily="Segoe UI Black"
               Foreground="White" 
               Padding="0" RenderTransformOrigin="0.5,0.5">
        <TextBlock.RenderTransform>
            <CompositeTransform/>
        </TextBlock.RenderTransform>
        <Interactivity:Interaction.Behaviors>
            <Core:DataTriggerBehavior Binding="{Binding TestTypeText}" ComparisonCondition="NotEqual" Value="{Binding TestTypeText}">
                <Media:ControlStoryboardAction Storyboard="{StaticResource FlashingText}" />
            </Core:DataTriggerBehavior>
        </Interactivity:Interaction.Behaviors>
    </TextBlock>
    

    上面的代码几乎是你的,我只编辑了行为,所以当TestTypeText改变时它会调用故事板。

    【讨论】:

    • 这非常有效。我想过使用 DataTriggerBehavior 但我不知道要使用什么 ComparisonCondition。我认为由于它们绑定到相同的属性,它会返回完全相同的值,因此永远不会满足比较条件。
    猜你喜欢
    • 1970-01-01
    • 2014-02-26
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2015-05-11
    • 2012-12-04
    • 2014-09-03
    相关资源
    最近更新 更多