开发者

Can I use Generics to Simply Wpf Notifiable Bindings?

Can I use VB.Net's generics to simply Wpf no开发者_开发问答tifiable bound controls?


Here's how to use VB.Net's Generics to create notifiable bound controls.

Class MainWindow

Public Sub New()

    InitializeComponent()

    Me.DataContext = Me

End Sub

Public Property NoNotify As String = "No notify"
Public Property DoesNotify As New NotifiableProperty(Of String)("DoesNotify")

Private Sub TestBtn_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles TestBtn.Click
    NoNotify = "Won't Show"
    DoesNotify.Value = "Notified!"
End Sub

End Class

Public Class NotifiableProperty(Of T)
    Implements System.ComponentModel.INotifyPropertyChanged
Sub New()
End Sub
Sub New(ByVal InitialValue As T)
    value = InitialValue
End Sub
Private m_Value As T
Public Property Value As T
    Get
        Return m_Value
    End Get
    Set(ByVal value As T)
        m_Value = value
        RaiseEvent PropertyChanged(Me, New System.ComponentModel.PropertyChangedEventArgs("Value"))
    End Set
End Property
    Private Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged
End Class

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <StackPanel>
            <Button Name="TestBtn" Content="Click to Test" Width="72" />
            <TextBox Name="NoNotifyTB" Text="{Binding NoNotify}" />
            <TextBox Name="DoesNotifyTB" Text="{Binding DoesNotify.Value}"/>
        </StackPanel>
    </Grid>
</Window>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜