【问题标题】:Can I use Generics to Simply Wpf Notifiable Bindings?我可以使用泛型来简单地 Wpf 通知绑定吗?
【发布时间】:2011-05-13 12:34:25
【问题描述】:

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

【问题讨论】:

  • 你应该接受你的问题的答案。

标签: wpf vb.net generics binding inotifypropertychanged


【解决方案1】:

以下是如何使用 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>

【讨论】:

  • DoesNotify 属性应为 ReadOnly
猜你喜欢
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-14
  • 2012-10-01
  • 1970-01-01
相关资源
最近更新 更多