数据绑定在WPF VB.NET中不起作用

数据绑定在WPF VB.NET中不起作用,wpf,vb.net,data-binding,Wpf,Vb.net,Data Binding,我一直在尝试处理数据绑定,但很难让它正常工作。有人能给我指出正确的方向吗 我正在使用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://sche

我一直在尝试处理数据绑定,但很难让它正常工作。有人能给我指出正确的方向吗

我正在使用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)
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上的更新。显然我错过了一些愚蠢的东西,但我还没有找到正确的资源。

试试这个

在页面资源下添加datacontext

<Page.DataContext>

  <local:clsItemGUI/>

</Page.DataContext>

(带模式=双向)


必须将
DataContext
分配给根布局(在我的示例中是
Grid
):

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

我认为您需要将类设置为页面数据上下文而不是resourceHmmm,我以为我已经这样做了?你能举例说明你的意思吗?谢谢你的快速回复,J·金。现在textblock读取“0”(以前为空),但仍然不更新。我正在逐步浏览代码,我可以看到我正在将force变量传递给Me.ForceValue。但是,我的两个断点:Return ForceValue和RaiseEvent PropertyChanged(我,New PropertyChangedEventArgs(name))…从未到达。Kmatyaszek,我尝试了这两个更改,但我的UI仍然没有更新。然而,我现在在“returnforcevalue”上点击断点——只有一次,当我第一次加载程序时(在UI出现之前)。@gadget00再次检查我的答案:)啊!现在我们在用煤气做饭。现在,当我逐步阅读代码时,我点击了UpdateForce函数,它触发Force()属性函数,然后触发OnPropertyChanged函数。但是,它仍然没有更新UI。我的用户界面现在经常读2.5。我相信用户界面只是冻结了,现在。。。但它应该在自己的线程上运行,不是吗?回到Windows窗体(在这里我比较舒服),我会加入一个应用程序.DoEvents(),但我知道这不再是必要的。@我为您创建的gadget00简单示例,您可以在这里下载它:
<TextBlock x:FieldModifier="public" x:Name="p_force" Text="{Binding Path=Force, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
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
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