简体   繁体   中英

Update TextBlock.Text On Opacity = 0.0

I want to change a TextBlock.Text when its Opacity value is 0.0. The TextBlock being animated (Opacity fade from 1.0 to 0.0 over 3 seconds, repeating and autoreversing) by a DoubleAnimation on a Storyboard. Is this even possible using the DoubleAnimation or Storyboard Events? I've tried changing the Text Property of the TextBlock in CurrentStateInvalidated and CurrentTimeInvalidated but those only fire once.

EDIT: Here's the Trigger I have so far. This does not produce the expected result.

public MainWindow()
    {
        InitializeComponent();
        this.Loaded += delegate
        {
            Style st = new Style();
            st.TargetType = typeof(TextBlock);
            DataTrigger t = new DataTrigger();
            t.Value = (double)0.0;
            t.Binding = new Binding()
            {
                Path = new PropertyPath("Opacity"),
                RelativeSource = RelativeSource.Self
            };
            Setter se = new Setter();
            se.Property = TextBlock.TextProperty;
            se.Value = GetTickerString();
            t.Setters.Add(se);
            st.Triggers.Clear();
            st.Triggers.Add(t);
            txblkTickerText1.Style = st;
        };
    }

If Opacity equals 0 no text is visible. I made simple example but it invokes text change once opacity equals 0.7

<Window.Triggers>
    <EventTrigger RoutedEvent="Loaded">
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetName="TextBlockElement" Storyboard.TargetProperty="Opacity" To="0.7"/>
            </Storyboard>
        </BeginStoryboard>
    </EventTrigger>
</Window.Triggers>
<StackPanel>
    <TextBlock Name="TextBlockElement">
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Text" Value="text"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Opacity}" Value="0.7">
                        <Setter Property="Text" Value="Opacity is 0.7"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</StackPanel>

Start the StoryBoard in code, then immediately start a DispatcherTimer for the amount of time it takes for opacity to go to zero. So, in the tick event of DispatcherTimer , you can set the text.

say,

void func()
{
    StoryBoard myStoryBoard;
    //configure it
    myStoryBoard.Begin();
    DispatcherTimer _timer = new DispatcherTimer();
    _timer.Interval = new TimeSpan(0,0,1);
    _timer.Tick += _timer_Tick;
    _timer.Start();
}

void _timer_Tick(object sender, EventArgs e)
{
    yourTextBlock.Text = "changedText";
    (sender as DispatcherTimer).Stop();
}

You may simply handle the Completed event of the Opacity animation:

DoubleAnimation animation = ...

animation.Completed +=
    (o, e) =>
    {
        textBlock.Text = "Opacity animation completed.";
    }; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM