首页 > 解决方案 > WPF 文本框绑定。VB.Net 2017

问题描述

场景:结构与索引随机访问文件相关联。

在启动时从构造函数中的文件加载数据,并自动为结构分配文件中的数据值,并自动填充 WPF 窗口中的文本框。如果用户在文本框中输入新值,结构也会更新。到目前为止,一切都很好。

当程序运行时用户从文件加载不同的数据集时,就会出现问题。这发生在按钮单击事件中。结构已更新,但此更改未反映在文本框中。

希望有人能对此有所了解。

WPF 和 VB.Net 都很新

Public Class MainWindow
Shared myStruct As aStruct
Public Structure aStruct
    Implements INotifyPropertyChanged
    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    Public year As Integer
    Public Property YearP As Integer
        Get
            Return myStruct.year
        End Get
        Set(value As Integer)
            myStruct.year = value
        End Set
    End Property

    Public salary as Integer
    Public Property SalaryP As Integer
        Get
            Return myStruct.salary
        End Get
        Set(value As Integer)
            myStruct.salary = value
        End Set
    End Property
   .......
   .......
 End Structure

Public Sub New()
    Me.New(".\indxfile.ptx", ".\datafile.dat")

    ' This call is required by the designer.
    'InitializeComponent() ' Moved

    ' Add any initialization after the InitializeComponent() call.
End Sub

Public Sub New(Optional _IndexFileName As String = "", Optional _DataFileName As String = "")

    ' This call is required by the designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

    Me.DataContext = myStruct

    Dim Retval As Integer
    Retval = GetRecord(2018, DataFileName, Len(myStruct), myStruct, IndexFilename)
    If Retval <> 0 Then
        MsgBox("Error")
    End If

End Class

<TextBox x:Name="Year" Text = "{Binding YearP, Mode = TwoWay}"

标签: wpf

解决方案


您的“设置”属性需要调用 PropertyChanged 事件,以便数据绑定更新。


推荐阅读