龙空技术网

vb.net在WPF中数据绑定,实现INotifyPropertyChanged的完整示例

守望计划 136

前言:

今天你们对“vbnet动态”可能比较注重,兄弟们都需要知道一些“vbnet动态”的相关内容。那么小编在网络上汇集了一些有关“vbnet动态””的相关文章,希望你们能喜欢,各位老铁们快快来学习一下吧!

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>

.vb文件如下

'导入引用Imports System.ComponentModelImports System.Runtime.CompilerServicesClass MainWindowPublic 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 PropertyEnd ClassDim AppSet As New AppSetsPublic Sub New()    ' 此调用是设计器所必需的。    InitializeComponent()    ' 在 InitializeComponent() 调用之后添加任何初始化。    DataContext = AppSetEnd SubPrivate Sub Button_Click(sender As Object, e As RoutedEventArgs)    AppSet.TmpStr = "哈哈哈哈哈"    AppSet.Checked = True    AppSet.BackColor = "red"End SubEnd Class

以上代码新建WPF应用复制到对应文件即可。本文为原创,转载请注明出处为桔子雨工作室。原文:WPF中的数据绑定技术用vb.net实现INotifyPropertyChanged的完整示例 – 桔子雨

标签: #vbnet动态