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>
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
似乎缺少一些东西。首先,未设置窗口的数据上下文。您可以使用构造函数执行此操作:
Public Sub New()
DataContext = oTime
End Sub
这允许您的视图查看 TimeUpdate 类的内容。
然后更改 XAML(直接绑定到 TimeUpdate 属性):
<Grid>
<TextBox x:Name="myTextBox"
Width="200" Height="50" Foreground="Black"
Text="{Binding Path=TimeUpdate}"></TextBox>
</Grid>
更新:另一种方法是在 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">
现在创建一个公共属性来访问对象:
Public Property OTime() As TimeUpdate
Get
Return oTime
End Get
Set
oTime = value
End Set
End Property
并将文本框绑定到它:
<Grid>
<TextBox x:Name="myTextBox"
Width="200" Height="50" Foreground="Black"
Text="{Binding Path=OTime.TimeUpdate}"></TextBox>
</Grid>
我认为您的数据上下文已关闭。 如果我错了,请纠正我,但看起来您将其设置为字符串值而不是对象。oTime 是否是您在 XAML 中创建的资源的对象?如果是这样,您将需要使用静态资源获取它。否则,您将需要实例化。
例如。
<TextBlock >
<TextBlock.DataContext>
<!-- This will instantiate the object-->
<local:TimeUpdate/>
</TextBlock.DataContext>
<TextBox x:Name="myTextBox" Width="200" Height="50" Foreground="Black" Text="{Binding Path=TimeUpdate}"></TextBox>
</TextBlock>
编辑:我刚刚注意到您在MainWindow类中实例化了一个TimeUpdate对象。在这种情况下,我会在代码隐藏中将窗口的 DataContext 设置为自身,并使 oTime 成为属性。这样,您就不必为TextBlock
设置数据上下文。它将使用父级的 DataContext,在本例中为 Window
。XAML 现在如下所示:
<TextBlock>
<TextBox x:Name="myTextBox" Width="200" Height="50" Foreground="Black" Text="{Binding Path=oTime.TimeUpdate}"/>
</TextBlock>
尝试更改以下内容:
Text="{Binding Path=oTime.TimeUpdate}"
自:
Text="{Binding Path=oTime.TimeUpdate, UpdateSourceTrigger="PropertyChanged"}"
尝试更改此设置
OnPropertyChanged("TimeUpdate")
对此
OnPropertyChanged("oTime")
此外,请确保已在根网格上设置了数据上下文
<Grid DataContext="{Binding RelativeSource={RelativeSource Mode=Self}, Path=.}">