1

在表达式混合(wpf)中对数据中的文本有效果(淡入>淡出>淡入)吗?

例子:

我有包含数据的表(比如说 SQL):

姓名:杰克

姓名:琼

名称:翡翠

我该怎么做才能让 Jack 显示 - 5 秒后 -> Jack 会淡出,然后 Jhon 会淡入......等等。

我知道如何连接 sql 并编写 c# 类并在 wpf 上使用它,但是我怎样才能从示例中获得效果呢?

4

2 回答 2

2

这是一个实现它的例子

一个简单的数据模型

 public class ModelList : List<string>
    {
        public ModelList()
        {
            Add("John");
            Add("Jack");
            Add("Sue");
        }

        public int CurrentIndex = 0;
        public string CurrentItem
        {
            get
            {
                return this[CurrentIndex];
            }
        }
    }

你的主窗口

      public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        void ContinueAnimation()
        {
            ModelList list = Resources["ModelList"] as ModelList;
            if ( list.CurrentIndex < (list.Count -1))
            {
                list.CurrentIndex += 1;
                Storyboard b = Resources["FadeOut"] as Storyboard;
                b.Begin();
            }
        }

        private void Start_Click(object sender, RoutedEventArgs e)
        {
            ContinueAnimation();
        }

        private void FadeOut_Completed(object sender, EventArgs e)
        {
            ContinueAnimation();    
        }

    }

您的主窗口 xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:app="clr-namespace:WpfApplication1"
        Title="MainWindow"
        Width="1000"
        Height="1000">
    <Window.Resources>
        <app:ModelList x:Key="ModelList" />
        <Storyboard x:Key="FadeOut" x:Name="FadeOut" Completed="FadeOut_Completed">
            <DoubleAnimation Duration="0:0:0.5"
                             Storyboard.TargetName="MyLabel"
                             Storyboard.TargetProperty="Opacity"
                             To="0" />
            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="MyLabel" Storyboard.TargetProperty="Text">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.5" Value="{Binding Source={StaticResource ModelList}, Path=CurrentItem}" />
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimation BeginTime="0:0:0.5"
                             Duration="0:0:1"
                             Storyboard.TargetName="MyLabel"
                             Storyboard.TargetProperty="Opacity"
                             To="1" />
        </Storyboard>
    </Window.Resources>
    <StackPanel>

        <TextBlock Name="MyLabel"
                   Width="100"
                   Height="24"
                   Background="AliceBlue"
                   Text="{Binding Source={StaticResource ModelList},
                                  Path=CurrentItem}" />

        <Button Name="Start"
                Height="30"
                HorizontalAlignment="Left"
                Click="Start_Click">
            Start
        </Button>

    </StackPanel>
</Window>
于 2012-07-31T17:29:10.700 回答
1

如果您有表情混合,您可以创建一个故事板并在故事板的不同时间设置不透明度。当应用于您的文本时,它将淡入淡出。

您可以挂钩Timeline.Completed 事件,然后将文本设置为下一个人并重新启动动画。我认为该页面上的示例也会对您有所帮助。

于 2012-07-31T17:00:55.093 回答