我一直在尝试处理数据绑定,但我无法使其正常工作。有人能指出我正确的方向吗?
我正在使用VS2012并在WPF / vb.net中编程。我有一个xaml UI,一个更新属性的类,以及运行我的主循环的模块。我正在尝试根据模块中的动态数据更新UI中的文本块。
以下是UI中重要的XAML:
<Page x:Class="pPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:BMI_WPF_v1"
mc:Ignorable="d"
Title="BMI">
<Page.Resources>
<local:clsItemGUI x:Key="clsitemgui" />
</Page.Resources>
<TextBlock x:FieldModifier="public" x:Name="p_force" Text="{Binding Path=Force, UpdateSourceTrigger=PropertyChanged}" />
以下是我在主模块中调用以尝试更新GUI的内容:
Dim bmiUI As New clsItemGUI
bmiUI.UpdateForce(currentForce)
这是用于更新GUI的类:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Public Class clsItemGUI
Implements INotifyPropertyChanged
Private ForceValue As Double
Sub New()
End Sub
Public Sub UpdateForce(ByVal inputForce As String)
Me.ForceValue = inputForce
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property Force() As Double
Get
Return ForceValue
End Get
Set(ByVal value As Double)
ForceValue = value
OnPropertyChanged("Force")
End Set
End Property
Protected Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class
看起来我正在更新ForceValue,但这并没有触发UI上的更新。显然我错过了一些愚蠢的东西,但我还没有偶然发现正确的资源。
答案 0 :(得分:0)
试试这个
在页面资源下添加datacontext
<Page.DataContext>
<local:clsItemGUI/>
</Page.DataContext>
(模式= TwoWay)
<TextBlock x:FieldModifier="public" x:Name="p_force" Text="{Binding Path=Force, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
答案 1 :(得分:0)
您必须将DataContext
分配给根布局(在我的示例中为Grid
):
<Grid DataContext="{StaticResource clsitemgui}">
<Grid.RowDefinitions>
<RowDefinition Height="30" />
<RowDefinition Height="30" />
</Grid.RowDefinitions>
<TextBlock x:FieldModifier="public" x:Name="p_force" Text="{Binding Path=Force, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
<Button x:Name="btn" Grid.Row="1" Content="Change" Click="Button_Click" />
</Grid>
或者您可以在绑定时设置Source
属性,如下所示:
<TextBlock x:FieldModifier="public" x:Name="p_force" Text="{Binding Path=Force, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
UpdateForce
方法中也有错误,您应该将新值分配给Force
属性而不是ForceValue
字段。将此值分配给字段OnPropertyChanged
时,不会调用它。
Public Class clsItemGUI
Implements INotifyPropertyChanged
Private ForceValue As Double
Sub New()
Force = 2.5
End Sub
Public Sub UpdateForce(ByVal inputForce As Double)
Me.Force = inputForce
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Public Property Force() As Double
Get
Return ForceValue
End Get
Set(ByVal value As Double)
ForceValue = value
OnPropertyChanged("Force")
End Set
End Property
Protected Sub OnPropertyChanged(ByVal name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
End Class
当你更新时,你应该在你指定为DataContext
的当前对象上这样做,所以你应该强制转换这个对象并在其上调用UpdateForce
函数:
Private Sub Button_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
Dim dt As clsItemGUI
dt = CType(btn.DataContext, clsItemGUI)
dt.UpdateForce(5.2)
End Sub