我已经搜索了一个星期以找到答案,但我不能。
由于某些原因,我必须实施Windows.Data.Binding
我的自我。所以我创建了一个类,并在其中放置一个私有的Binding
对象:
Public Class Binding
Inherits MarkupExtension
Private _Binding As New Windows.Data.Binding
#Region "Properties"
Public Property ElementName() As String
Get
Return _Binding.ElementName
End Get
Set(value As String)
_Binding.ElementName = value
End Set
End Property
Public Property Mode() As BindingMode
Get
Return _Binding.Mode
End Get
Set(value As BindingMode)
_Binding.Mode = value
End Set
End Property
Public Property Source() As Object
Get
Return _Binding.Source
End Get
Set(value As Object)
_Binding.Source = value
End Set
End Property
#End Region
'*Other Properties of Windows.Data.Binding go inside region*
Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
If serviceProvider Is Nothing Then
Return "Design Time"
Else
Dim k = _Binding.ProvideValue(serviceProvider)
Return k
End If
End Function
End Class
有效。
我需要更改ProvideValue
中的一些实现,为此,我需要找到绑定的来源。例如,我在XAML中有一个像这样的对象:
<TextBlock Text={local:Binding Path=UserID} />
好的,我的TextBlock
绑定到{{1>} 什么...... ??
问题是绑定的来源是什么?和如何在UserID
方法中访问?
感谢所有
答案 0 :(得分:1)
这是一个如何使用服务提供商来获取目标的示例。绑定来源
Public Overrides Function ProvideValue(serviceProvider As IServiceProvider) As Object
Dim service As IProvideValueTarget = DirectCast(serviceProvider.GetService(GetType(IProvideValueTarget)), IProvideValueTarget)
Dim targetObject As DependencyObject = TryCast(service.TargetObject, DependencyObject)
Dim targetProperty As DependencyProperty = TryCast(service.TargetProperty, DependencyProperty)
'find source of the binding'
Dim targetElement As FrameworkElement = TryCast(targetObject, FrameworkElement)
If targetElement IsNot Nothing Then
Dim sourceObject As Object = targetElement.DataContext
'your logic here'
End If
End Function
我建议只有在使用转换器和格式时无法实现相同的扩展时才创建自己的绑定扩展,并且如果有特定需要这样做的话。
请阅读此处以获取有关IServiceProvider http://msdn.microsoft.com/en-us/library/ee621338.aspx
的更多信息