[英]Update TextBlock.Text On Opacity = 0.0
我想更改其不透明度值为0.0时的TextBlock.Text。 由Storyboard上的DoubleAnimation动画化TextBlock(不透明度在3秒钟内从1.0渐变为0.0,重复和自动反转)。 使用DoubleAnimation或Storyboard Events甚至可能吗? 我试过在CurrentStateInvalidated和CurrentTimeInvalidated中更改TextBlock的Text属性,但是这些仅触发一次。
编辑:这是我到目前为止的触发器。 这不会产生预期的结果。
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;
};
}
如果不透明度等于0,则看不到任何文本。 我做了一个简单的示例,但是一旦不透明度等于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>
用代码启动StoryBoard
,然后立即启动DispatcherTimer
,使不透明度变为零所花费的时间。 因此,在DispatcherTimer
的tick
事件中,您可以设置文本。
说,
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();
}
您可以简单地处理Opacity动画的Completed
事件:
DoubleAnimation animation = ...
animation.Completed +=
(o, e) =>
{
textBlock.Text = "Opacity animation completed.";
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.