SDa*_*nks 0 vb.net wpf binding inotifypropertychanged
使用WPF和VB.net,我想用当前日期和时间更新文本块中的文本框.我使用计时器,它似乎正在触发,并将对象属性设置为"现在".我正在使用iNotifyPropertyChanged.
我得到的只是一个没有数据的空文本框.你能帮我吗?也许我的背景是关闭的?
XAML
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBlock DataContext="oTime">
<TextBox x:Name="myTextBox"
Width="200" Height="50" Foreground="Black"
Text="{Binding Path=oTime.TimeUpdate}"></TextBox>
</TextBlock>
</Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)
VB代码
Imports System.ComponentModel
Imports System.Windows.Threading
Class MainWindow
Public oTime As TimeUpdate = New TimeUpdate
Private dpTimer As DispatcherTimer
Private Sub TextBlock_SourceUpdated(ByVal sender As System.Object, ByVal e As System.Windows.Data.DataTransferEventArgs)
End Sub
Private Sub MainWindow_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
dpTimer = New DispatcherTimer
dpTimer.Interval = TimeSpan.FromMilliseconds(1000)
AddHandler dpTimer.Tick, AddressOf TickMe
dpTimer.Start()
End Sub
Private Sub TickMe()
oTime.TimeUpdate = Now.ToString
Debug.Print(oTime.TimeUpdate)
End Sub
End Class
Public Class TimeUpdate
Implements INotifyPropertyChanged
Private sTime As String
'Declare the Event
Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
Public Property TimeUpdate() As String
Get
Return sTime
End Get
Set(ByVal value As String)
sTime = value
'Call onPropertyChanged whenever the property is updated
OnPropertyChanged("TimeUpdate")
End Set
End Property
Protected Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class
Run Code Online (Sandbox Code Playgroud)
似乎很少有东西丢失.首先,未设置窗口的DataContext.你可以做构造函数:
Public Sub New()
DataContext = oTime
End Sub
Run Code Online (Sandbox Code Playgroud)
这允许您的视图查看TimeUpdate类的内容.
然后更改您的XAML(直接绑定到TimeUpdate属性):
<Grid>
<TextBox x:Name="myTextBox"
Width="200" Height="50" Foreground="Black"
Text="{Binding Path=TimeUpdate}"></TextBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
更新: 替代方法是在Window标记中添加DataContext行.这样,您的MainWindow类对视图可见,您可以绑定到公共属性.
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
Run Code Online (Sandbox Code Playgroud)
现在创建一个公共属性来访问该对象:
Public Property OTime() As TimeUpdate
Get
Return oTime
End Get
Set
oTime = value
End Set
End Property
Run Code Online (Sandbox Code Playgroud)
并将文本框绑定到它:
<Grid>
<TextBox x:Name="myTextBox"
Width="200" Height="50" Foreground="Black"
Text="{Binding Path=OTime.TimeUpdate}"></TextBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)