试试这个。
后面的代码。
public partial class dtfromdataset : Window
{
public dtfromdataset()
{
InitializeComponent();
this.DataContext = this;
time.Interval = 5000;
time.Elapsed += new ElapsedEventHandler(time_Elapsed);
time.Start();
}
Timer time = new Timer();
void time_Elapsed(object sender, ElapsedEventArgs e)
{
Dispatcher.BeginInvoke(new Action(() =>
{
StatusBarText = "Time is " + DateTime.Now.ToString("ddd-MM-yy HH:mm:ss tt");
}));
}
private DataTable dt = new DataTable();
public string StatusBarText
{
get { return (string)GetValue(StatusBarTextProperty); }
set { SetValue(StatusBarTextProperty, value); }
}
// Using a DependencyProperty as the backing store for StatusBarText. This enables animation, styling, binding, etc...
public static readonly DependencyProperty StatusBarTextProperty =
DependencyProperty.Register("StatusBarText", typeof(string), typeof(dtfromdataset), new UIPropertyMetadata(""));
}
Xaml
<Grid Name="stackPanel1">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="224*" />
</Grid.RowDefinitions>
<TextBlock Name="statusText"
Grid.Row="0"
HorizontalAlignment="Stretch"
Background="Silver"
FontSize="20"
Text="{Binding Path=StatusBarText,
NotifyOnTargetUpdated=True}"
TextAlignment="Center">
<TextBlock.Triggers>
<EventTrigger RoutedEvent="Binding.TargetUpdated">
<BeginStoryboard>
<Storyboard>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="Opacity">
<EasingDoubleKeyFrame KeyTime="0" Value="0" />
<EasingDoubleKeyFrame KeyTime="0:0:0.25" Value="1" />
<EasingDoubleKeyFrame KeyTime="0:0:4" Value="1" />
<EasingDoubleKeyFrame KeyTime="0:0:5" Value="0" />
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</TextBlock.Triggers>
</TextBlock>
</Grid>