1

问题是如何设置依赖属性以将其绑定到任何其他依赖属性并稍后释放它。

UserControl1 中有一个依赖属性

#Region "INotifyPropertyChanged Members"

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

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

#End Region



#Region "BorderSize"

    Dim _BorderSize As Double = 1

    ''' <summary>
    ''' Gets or sets the BorderSize property. This observable property 
    ''' indicates ....
    ''' </summary>
    Public Overridable Property BorderSize() As Double
        Get
            Return _BorderSize
        End Get
        Set(ByVal value As Double)
            If _BorderSize <> value Then
                _BorderSize = value
                RaisePropertyChanged("BorderSize")
            End If
        End Set
    End Property

#End Region

公共共享类中的一个依赖属性。

Public Class MyPublicClass
    Implements INotifyPropertyChanged

#Region "INotifyPropertyChanged Members"

    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

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

#End Region


#Region "BorderSize"

    Dim _BorderSize As Double = 1

    ''' <summary>
    ''' Gets or sets the BorderSize property. This observable property 
    ''' indicates ....
    ''' </summary>
    Public Property BorderSize() As Double
        Get
            Return _BorderSize
        End Get
        Set(ByVal value As Double)
            If _BorderSize <> value Then
                _BorderSize = value
                RaisePropertyChanged("BorderSize")
            End If
        End Set
    End Property

#End Region

End Class

在 MainWindow.xaml.vb(代码隐藏)上,需要取消注册旧的用户控件并注册新的选定用户控件,以使用新用户控件的属性设置 Shared MyPublicClass。

如何在代码隐藏中手动取消注册和注册依赖属性?

*解决了*

Dim myDataObject As New MyData(DateTime.Now)
Dim myBinding As New Binding("MyDataProperty")
myBinding.Source = myDataObject
BindingOperations.SetBinding(myText, TextBlock.TextProperty, myBinding)
4

1 回答 1

2

您需要创建一个Binding 对象,然后使用SetBinding 在目标上设置。

更多信息在这里:http: //msdn.microsoft.com/en-us/library/ms742863.aspx

于 2012-12-29T13:36:19.977 回答