<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:src="clr-namespace:WpfApplication1.DigitalClock"
Title="Digital Clock"
SizeToContent="WidthAndHeight">
<Window.Resources>
<src:ClockTicker1 x:Key="clock" />
</Window.Resources>
<Window.Content>
<Binding Source="{StaticResource clock}" Path="DateTime" />
</Window.Content>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Windows
Imports System.Windows.Threading
Namespace WpfApplication1.DigitalClock
Public Class ClockTicker1
Inherits DependencyObject
Public Shared DateTimeProperty As DependencyProperty = DependencyProperty.Register("DateTime", GetType(DateTime), GetType(ClockTicker1))
Public Property DateTime() As DateTime
Get
Return CType(GetValue(DateTimeProperty), DateTime)
End Get
Set
SetValue(DateTimeProperty, value)
End Set
End Property
Public Sub New()
Dim timer As New DispatcherTimer()
AddHandler timer.Tick, AddressOf TimerOnTick
timer.Interval = TimeSpan.FromSeconds(1)
timer.Start()
End Sub
Private Sub TimerOnTick(sender As Object, args As EventArgs)
DateTime = DateTime.Now
End Sub
End Class
End Namespace
|