Reputation: 77
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;
};
}
Upvotes: 0
Views: 337
Reputation: 128013
You may simply handle the Completed
event of the Opacity animation:
DoubleAnimation animation = ...
animation.Completed +=
(o, e) =>
{
textBlock.Text = "Opacity animation completed.";
};
Upvotes: 0
Reputation: 3448
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>
Upvotes: 1
Reputation: 2010
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();
}
Upvotes: 0