我可以使用泛型来实现简单的 Wpf 可通知绑定吗?

发布于 2024-10-04 08:30:58 字数 44 浏览 1 评论 0原文

我可以使用 VB.Net 的泛型来简单地使用 Wpf 可通知的绑定控件吗?

Can I use VB.Net's generics to simply Wpf notifiable bound controls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-10-11 08:30:58

下面介绍如何使用 VB.Net 的泛型来创建可通知的绑定控件。

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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文