WPF中的数据绑定技术用vb.net实现INotifyPropertyChanged的完整示例

WPF实现INotifyPropertyChanged数据更新通知很多都是C#的,自用VB版的示例如下:

xaml文件

<Grid>
    <StackPanel>
        <Label Content="字符串"/>
        <TextBox Text="{Binding TmpStr,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
        <Label Content="boolean"/>
        <CheckBox IsChecked="{Binding Checked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Content="自动关机"></CheckBox>
        <CheckBox IsChecked="{Binding Checked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Content="自动关机"></CheckBox>
        <Button Content="测试" Click="Button_Click"></Button>
        <Label Content="测试BackColor"/>
        <TextBox Text="{Binding BackColor,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" ToolTip="只要更新数据我就通知更新"></TextBox>
        <!--Mode默认双向绑定,更新通知事件UpdateSourceTrigger默认为失去焦点
        PropertyChanged:变化即刻通知
        LostFocus:失去焦点通知
        -->
        <TextBox Text="{Binding BackColor}" Background="{Binding BackColor}" ToolTip="我要失去焦点才通知数据更新"></TextBox>
    </StackPanel>
</Grid>

xaml.vb如下

'导入引用
Imports System.ComponentModel
Imports System.Runtime.CompilerServices
Class MainWindow
Public Class AppSets
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    Protected Function SetProperty(Of T)(ByRef backingStore As T, value As T, <CallerMemberName> Optional propertyName As String = "", Optional onChanged As Action = Nothing, Optional force As Boolean = False) As Boolean
        If Not force AndAlso EqualityComparer(Of T).[Default].Equals(backingStore, value) Then
            Return False
        End If

        backingStore = value
        onChanged?.Invoke()
        OnPropertyChanged(propertyName)
        Return True
    End Function

    Protected Sub OnPropertyChanged(name As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
    End Sub


    '定义属性
    Private _TmpStr As String

    Public Property TmpStr As String
        Get
            Return _TmpStr
        End Get
        Set(value As String)
            SetProperty(_TmpStr, value)
        End Set
    End Property


    Private _Checked As Boolean
    Public Property Checked As Boolean
        Get
            Return _Checked
        End Get
        Set(value As Boolean)
            SetProperty(_Checked, value)
        End Set
    End Property

    Private _BackColor As String = "green"  '默认值
    Public Property BackColor As String
        Get
            Return _BackColor
        End Get
        Set(value As String)
            SetProperty(_BackColor, value)
        End Set
    End Property
End Class

Dim AppSet As New AppSets
Public Sub New()

    ' 此调用是设计器所必需的。
    InitializeComponent()

    ' 在 InitializeComponent() 调用之后添加任何初始化。
    DataContext = AppSet
End Sub

Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
    AppSet.TmpStr = "哈哈哈哈哈"
    AppSet.Checked = True
    AppSet.BackColor = "red"
End Sub
End Class

转载请注明:转载自桔子雨工作室。WPF中的数据绑定技术用vb.net实现INotifyPropertyChanged的完整示例

未经允许不得转载:桔子雨工作室 » WPF中的数据绑定技术用vb.net实现INotifyPropertyChanged的完整示例
分享到: 生成海报
一个数字化服务提供商

承接外贸建站,软件APP开发

部分产品联系我们
切换注册

登录

忘记密码 ?

您也可以使用第三方帐号快捷登录

切换登录

注册

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码