0

我一直在尝试处理数据绑定,但无法使其正常工作。有人可以指出我正确的方向吗?

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

4

2 回答 2

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

当您更新时,您应该对分配为 a 的当前对象执行此操作,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
于 2013-03-18T20:35:06.120 回答
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}" />
于 2013-03-18T20:30:38.303 回答