【问题标题】:Update TextBlock.Text On Opacity = 0.0在不透明度 = 0.0 时更新 TextBlock.Text
【发布时间】:2015-02-12 18:15:22
【问题描述】:

当 Opacity 值为 0.0 时,我想更改 TextBlock.Text。由情节提要上的 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 时为文本放置一个设置器

标签: c# wpf


【解决方案1】:

在代码中启动StoryBoard,然后立即启动DispatcherTimer,直到不透明度变为零所需的时间。所以,在DispatcherTimertick事件中,可以设置文字。

说,

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();
}

【讨论】:

  • 如果您想保持故事板永无止境,请相应修改此设置,即如果您拥有无限故事板,请不要停止 DispatcherTimer
  • 简单而有效。谢谢。
【解决方案2】:

如果不透明度等于 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>

【讨论】:

    【解决方案3】:

    您可以简单地处理 Opacity 动画的Completed 事件:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 2013-03-04
      • 1970-01-01
      • 2018-06-24
      • 2012-09-07
      • 2016-08-31
      • 2012-09-20
      相关资源
      最近更新 更多