- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 VB.Net 在 Silverlight 中实现多重绑定(bind)。我在 C# here 中找到了一个很好的实现引用。 .我花了一些时间尝试使用各种转换器将其迁移到 VB.Net,但我仍然没有让它正常工作。所以..
我正在寻找一些引用资料来举例说明如何在 VB.Net 中完成 MultiBinding。 .
还有一个使用 Silverlight 5 beta 的例子也可以(我在 Stack Overflow 上的一篇文章 right here 中读到它支持多重绑定(bind))。
最佳答案
我已经将该示例翻译成 Silverlight 的 VB.NET,可以从 here 下载。 .为了后代,下面列出了代码。请记住,两者仍受原作者指定的任何许可条款的约束。
核心代码
BindingUtil.vb
Imports System.Collections.Generic
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Windows.Data
Imports System.ComponentModel
Namespace SLMultiBinding
''' <summary>
''' Provides a mechanism for attaching a MultiBinding to an element
''' </summary>
Public Class BindingUtil
#Region "DataContextPiggyBack attached property"
''' <summary>
''' DataContextPiggyBack Attached Dependency Property, used as a mechanism for exposing
''' DataContext changed events
''' </summary>
Public Shared ReadOnly DataContextPiggyBackProperty As DependencyProperty = DependencyProperty.RegisterAttached("DataContextPiggyBack", GetType(Object), GetType(BindingUtil), New PropertyMetadata(Nothing, New PropertyChangedCallback(AddressOf OnDataContextPiggyBackChanged)))
Public Shared Function GetDataContextPiggyBack(d As DependencyObject) As Object
Return DirectCast(d.GetValue(DataContextPiggyBackProperty), Object)
End Function
Public Shared Sub SetDataContextPiggyBack(d As DependencyObject, value As Object)
d.SetValue(DataContextPiggyBackProperty, value)
End Sub
''' <summary>
''' Handles changes to the DataContextPiggyBack property.
''' </summary>
Private Shared Sub OnDataContextPiggyBackChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim targetElement As FrameworkElement = TryCast(d, FrameworkElement)
' whenever the targeElement DataContext is changed, copy the updated property
' value to our MultiBinding.
Dim relay As MultiBindings = GetMultiBindings(targetElement)
relay.SetDataContext(targetElement.DataContext)
End Sub
#End Region
#Region "MultiBindings attached property"
Public Shared Function GetMultiBindings(obj As DependencyObject) As MultiBindings
Return DirectCast(obj.GetValue(MultiBindingsProperty), MultiBindings)
End Function
Public Shared Sub SetMultiBindings(obj As DependencyObject, value As MultiBindings)
obj.SetValue(MultiBindingsProperty, value)
End Sub
Public Shared ReadOnly MultiBindingsProperty As DependencyProperty = DependencyProperty.RegisterAttached("MultiBindings", GetType(MultiBindings), GetType(BindingUtil), New PropertyMetadata(Nothing, AddressOf OnMultiBindingsChanged))
''' <summary>
''' Invoked when the MultiBinding property is set on a framework element
''' </summary>
Private Shared Sub OnMultiBindingsChanged(depObj As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim targetElement As FrameworkElement = TryCast(depObj, FrameworkElement)
' bind the target elements DataContext, to our DataContextPiggyBack property
' this allows us to get property changed events when the targetElement
' DataContext changes
targetElement.SetBinding(DataContextPiggyBackProperty, New Binding())
Dim bindings As MultiBindings = GetMultiBindings(targetElement)
bindings.Initialize(targetElement)
End Sub
#End Region
End Class
End Namespace
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Imports System.Globalization
Namespace SLMultiBinding
''' <summary>
''' see: http://msdn.microsoft.com/en-us/library/system.windows.data.imultivalueconverter.aspx
''' </summary>
Public Interface IMultiValueConverter
Function Convert(values As Object(), targetType As Type, parameter As Object, culture As CultureInfo) As Object
Function ConvertBack(value As Object, targetTypes As Type(), parameter As Object, culture As CultureInfo) As Object()
End Interface
End Namespace
Imports System.Diagnostics
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Collections.ObjectModel
Imports System.Windows.Markup
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Globalization
Namespace SLMultiBinding
''' <summary>
''' Implements MultiBinding by creating a BindingSlave instance for each of the Bindings.
''' PropertyChanged events for the BindingSlae.Value property are handled, and the IMultiValueConveter
''' is used to compute the converted value.
''' </summary>
<ContentProperty("Bindings")> _
Public Class MultiBinding
Inherits Panel
Implements INotifyPropertyChanged
''' <summary>
''' Indicates whether the converted value property is currently being updated
''' as a result of one of the BindingSlave.Value properties changing
''' </summary>
Private _updatingConvertedValue As Boolean
#Region "ConvertedValue dependency property"
Public Shared ReadOnly ConvertedValueProperty As DependencyProperty = DependencyProperty.Register("ConvertedValue", GetType(Object), GetType(MultiBinding), New PropertyMetadata(Nothing, AddressOf OnConvertedValuePropertyChanged))
''' <summary>
''' This dependency property is set to the resulting output of the
''' associated Converter.
''' </summary>
Public Property ConvertedValue() As Object
Get
Return GetValue(ConvertedValueProperty)
End Get
Set(value As Object)
SetValue(ConvertedValueProperty, value)
End Set
End Property
Private Shared Sub OnConvertedValuePropertyChanged(depObj As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim relay As MultiBinding = TryCast(depObj, MultiBinding)
Debug.Assert(relay IsNot Nothing)
relay.OnConvertedValuePropertyChanged()
End Sub
''' <summary>
''' Handles propety changes for the ConvertedValue property
''' </summary>
Private Sub OnConvertedValuePropertyChanged()
OnPropertyChanged("ConvertedValue")
' if the value is being updated, but not due to one of the multibindings
' then the target property has changed.
If Not _updatingConvertedValue Then
' convert back
Dim convertedValues As Object() = Converter.ConvertBack(ConvertedValue, Nothing, ConverterParameter, CultureInfo.InvariantCulture)
' update all the binding slaves
If Children.Count = convertedValues.Length Then
For index As Integer = 0 To convertedValues.Length - 1
DirectCast(Children(index), BindingSlave).Value = convertedValues(index)
Next
End If
End If
End Sub
#End Region
#Region "CLR properties"
''' <summary>
''' The BindingMode
''' </summary>
Public Property Mode() As BindingMode
Get
Return m_Mode
End Get
Set(value As BindingMode)
m_Mode = value
End Set
End Property
Private m_Mode As BindingMode
''' <summary>
''' The target property on the element which this MultiBinding is assocaited with.
''' </summary>
Public Property TargetProperty() As String
Get
Return m_TargetProperty
End Get
Set(value As String)
m_TargetProperty = value
End Set
End Property
Private m_TargetProperty As String
''' <summary>
''' The Converter which is invoked to compute the result of the multiple bindings
''' </summary>
Public Property Converter() As IMultiValueConverter
Get
Return m_Converter
End Get
Set(value As IMultiValueConverter)
m_Converter = value
End Set
End Property
Private m_Converter As IMultiValueConverter
''' <summary>
''' The configuration parameter supplied to the converter
''' </summary>
Public Property ConverterParameter() As Object
Get
Return m_ConverterParameter
End Get
Set(value As Object)
m_ConverterParameter = value
End Set
End Property
Private m_ConverterParameter As Object
''' <summary>
''' The bindings, the result of which are supplied to the converter.
''' </summary>
Public Property Bindings() As BindingCollection
Get
Return m_Bindings
End Get
Set(value As BindingCollection)
m_Bindings = value
End Set
End Property
Private m_Bindings As BindingCollection
#End Region
Public Sub New()
Bindings = New BindingCollection()
End Sub
''' <summary>
''' Invoked when any of the BindingSlave's Value property changes.
''' </summary>
Private Sub SlavePropertyChanged(sender As Object, e As PropertyChangedEventArgs)
UpdateConvertedValue()
End Sub
''' <summary>
''' Uses the Converter to update the ConvertedValue in order to reflect
''' the current state of the bindings.
''' </summary>
Private Sub UpdateConvertedValue()
Dim values As New List(Of Object)()
For Each slave As BindingSlave In Children
values.Add(slave.Value)
Next
_updatingConvertedValue = True
ConvertedValue = Converter.Convert(values.ToArray(), GetType(Object), ConverterParameter, CultureInfo.CurrentCulture)
_updatingConvertedValue = False
End Sub
''' <summary>
''' Creates a BindingSlave for each Binding and binds the Value
''' accordingly.
''' </summary>
Friend Sub Initialise(targetElement As FrameworkElement)
Children.Clear()
For Each binding As Binding In Bindings
Dim slave As BindingSlave
' create a binding slave instance
If Not String.IsNullOrEmpty(binding.ElementName) Then
' create an element name binding slave, this slave will resolve the
' binding source reference and construct a suitable binding.
slave = New ElementNameBindingSlave(targetElement, binding)
Else
slave = New BindingSlave()
slave.SetBinding(BindingSlave.ValueProperty, binding)
End If
AddHandler slave.PropertyChanged, AddressOf SlavePropertyChanged
Children.Add(slave)
Next
End Sub
#Region "INotifyPropertyChanged Members"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
#End Region
End Class
''' <summary>
''' A simple element with a single Value property, used as a 'slave'
''' for a Binding.
''' </summary>
Public Class BindingSlave
Inherits FrameworkElement
Implements INotifyPropertyChanged
#Region "Value"
Public Shared ReadOnly ValueProperty As DependencyProperty = DependencyProperty.Register("Value", GetType(Object), GetType(BindingSlave), New PropertyMetadata(Nothing, AddressOf OnValueChanged))
Public Property Value() As Object
Get
Return GetValue(ValueProperty)
End Get
Set(value As Object)
SetValue(ValueProperty, value)
End Set
End Property
Private Shared Sub OnValueChanged(depObj As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim slave As BindingSlave = TryCast(depObj, BindingSlave)
Debug.Assert(slave IsNot Nothing)
slave.OnPropertyChanged("Value")
End Sub
#End Region
#Region "INotifyPropertyChanged Members"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Protected Sub OnPropertyChanged(name As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(name))
End Sub
#End Region
End Class
''' <summary>
''' A binding slave that performs 'ElementName' binding.
''' </summary>
Public Class ElementNameBindingSlave
Inherits BindingSlave
Private _multiBindingTarget As FrameworkElement
''' <summary>
''' The source element named in the ElementName binding
''' </summary>
Private _elementNameSource As FrameworkElement
Private _binding As Binding
Public Sub New(target As FrameworkElement, binding As Binding)
_multiBindingTarget = target
_binding = binding
' try to locate the named element
ResolveElementNameBinding()
AddHandler _multiBindingTarget.LayoutUpdated, AddressOf MultiBindingTarget_LayoutUpdated
End Sub
''' <summary>
''' Try to locate the named element. If the element can be located, create the required
''' binding.
''' </summary>
Private Sub ResolveElementNameBinding()
_elementNameSource = TryCast(_multiBindingTarget.FindName(_binding.ElementName), FrameworkElement)
If _elementNameSource IsNot Nothing Then
SetBinding(BindingSlave.ValueProperty, New Binding() With { _
.Source = _elementNameSource, _
.Path = _binding.Path, _
.Converter = _binding.Converter, _
.ConverterParameter = _binding.ConverterParameter _
})
End If
End Sub
Private Sub MultiBindingTarget_LayoutUpdated(sender As Object, e As EventArgs)
' try to locate the named element
ResolveElementNameBinding()
End Sub
End Class
Friend Delegate Sub BindingCollectionChangedCallback()
Public Class BindingCollection
Inherits Collection(Of BindingBase)
' Fields
' TODO: Private ReadOnly _collectionChangedCallback As BindingCollectionChangedCallback
Protected Overrides Sub ClearItems()
MyBase.ClearItems()
OnBindingCollectionChanged()
End Sub
Protected Overrides Sub InsertItem(index As Integer, item As BindingBase)
If item Is Nothing Then
Throw New ArgumentNullException("item")
End If
ValidateItem(item)
MyBase.InsertItem(index, item)
OnBindingCollectionChanged()
End Sub
Private Sub OnBindingCollectionChanged()
' TODO: RaiseEvent _collectionChangedCallback()
End Sub
Protected Overrides Sub RemoveItem(index As Integer)
MyBase.RemoveItem(index)
OnBindingCollectionChanged()
End Sub
Protected Overrides Sub SetItem(index As Integer, item As BindingBase)
If item Is Nothing Then
Throw New ArgumentNullException("item")
End If
ValidateItem(item)
MyBase.SetItem(index, item)
OnBindingCollectionChanged()
End Sub
Private Shared Sub ValidateItem(binding As BindingBase)
If Not (TypeOf binding Is Binding) Then
Throw New NotSupportedException("BindingCollectionContainsNonBinding")
End If
End Sub
End Class
End Namespace
Imports System.Collections.ObjectModel
Imports System.Linq
Imports System.Reflection
Imports System.Windows
Imports System.Windows.Data
Imports System.Windows.Markup
Namespace SLMultiBinding
''' <summary>
''' Manages the construction of multiple MultiBinding instances
''' </summary>
<ContentProperty("Bindings")> _
Public Class MultiBindings
Inherits FrameworkElement
Private _targetElement As FrameworkElement
''' <summary>
''' Gets / sets the collection of MultiBindings
''' </summary>
Public Property Bindings() As ObservableCollection(Of MultiBinding)
Get
Return m_Bindings
End Get
Set(value As ObservableCollection(Of MultiBinding))
m_Bindings = Value
End Set
End Property
Private m_Bindings As ObservableCollection(Of MultiBinding)
Public Sub New()
Bindings = New ObservableCollection(Of MultiBinding)()
End Sub
''' <summary>
''' Sets the DataContext of each of the MultiBinding instances
''' </summary>
Public Sub SetDataContext(dataContext As Object)
For Each relay As MultiBinding In Bindings
relay.DataContext = dataContext
Next
End Sub
''' <summary>
''' Initialises each of the MultiBindings, and binds their ConvertedValue
''' to the given target property.
''' </summary>
Public Sub Initialize(targetElement As FrameworkElement)
_targetElement = targetElement
Const DpFlags As BindingFlags = BindingFlags.[Public] Or BindingFlags.[Static] Or BindingFlags.FlattenHierarchy
For Each relay As MultiBinding In Bindings
relay.Initialise(targetElement)
' find the target dependency property
Dim targetType As Type = Nothing
Dim targetProperty As String = Nothing
' assume it is an attached property if the dot syntax is used.
If relay.TargetProperty.Contains(".") Then
' split to find the type and property name
Dim parts As String() = relay.TargetProperty.Split("."c)
targetType = Type.[GetType]("System.Windows.Controls." & parts(0) & ", System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e")
targetProperty = parts(1)
Else
targetType = targetElement.[GetType]()
targetProperty = relay.TargetProperty
End If
Dim sourceFields As FieldInfo() = targetType.GetFields(DpFlags)
Dim targetDependencyPropertyField As FieldInfo = sourceFields.First(Function(i) i.Name = targetProperty & "Property")
Dim targetDependencyProperty As DependencyProperty = TryCast(targetDependencyPropertyField.GetValue(Nothing), DependencyProperty)
' bind the ConvertedValue of our MultiBinding instance to the target property
' of our targetElement
Dim binding As New Binding("ConvertedValue") With { _
.Source = relay, _
.Mode = relay.Mode _
}
targetElement.SetBinding(targetDependencyProperty, Binding)
Next
End Sub
End Class
End Namespace
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Ink
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Shapes
Namespace SLMultiBinding
Public Class TitleConverter
Implements IMultiValueConverter
#Region "IMultiValueConverter Members"
Public Function Convert(values As Object(), targetType As Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements IMultiValueConverter.Convert
Dim forename As String = TryCast(values(0), String)
Dim surname As String = TryCast(values(1), String)
Return String.Format("{0}, {1}", surname, forename)
End Function
Public Function ConvertBack(value As Object, targetTypes As Type(), parameter As Object, culture As System.Globalization.CultureInfo) As Object() Implements IMultiValueConverter.ConvertBack
Dim source As String = TryCast(value, String)
Dim pos = source.IndexOf(", ")
Dim forename As String = source.Substring(pos + 2)
Dim surname As String = source.Substring(0, pos)
Return New Object() {forename, surname}
End Function
#End Region
End Class
End Namespace
关于.net - 使用 VB.Net 在 Silverlight 中进行多重绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6681671/
我有一个 foo 类,它有一个 bar 方法,它接受可调用的东西(函数指针/仿函数)。这个可调用的东西应该作为绑定(bind)元素传递给另一个方法 doit 和第三个方法 bar_cb 方法。 #in
我正在尝试在我的 WPF 4.0 应用程序(使用 VS 2010 Pro RTM)中创建自定义 TabItem 模板/样式,但尽管一切似乎都正常工作,但我注意到跟踪窗口中存在绑定(bind)错误。 我
作为一名刚接触 Android 的开发人员,我想我可能误解了绑定(bind)服务。 我创建了一项服务来结束对服务器的访问。作为此服务的一部分,该服务正在监听多播地址,以识别本地网络上的设备何时出现和消
这个问题在这里已经有了答案: What is the use of the JavaScript 'bind' method? (23 个回答) 关闭 7 年前。 所以我一直在尝试了解一些 JS 上
我不明白这三种语法之间的区别: where a = f (b) do a <- f (b) do let a = f (b) 我确实明白了a <- f(b)与其他两个不同,在大多数情况下,我尝试了所有
我在将 Cocoa 项目从手动同步接口(interface)模型转换为绑定(bind)模型时遇到问题,这样我就不必担心接口(interface)粘合代码。 我关注了 CocoaDevCentral C
我正在尝试找出一种好的方法来对处理大数据集的代码进行并行化,然后将结果数据导入 RavenDb。 数据处理受 CPU 限制和数据库导入 IO 限制。 我正在寻找一种解决方案,以对 Environmen
我正在 foreach 循环中生成单选按钮。我试图将选中的属性绑定(bind)到父级中的基本可观察值。不幸的是,当单击单选按钮时,父级的属性似乎没有在单击处理程序中更新。 基于一些previous w
在我的 Windows Phone 应用程序中,我有两个 LongListSelectors并排在页面上。我想做到这一点,以便当用户滚动其中一个时,另一个滚动相同的量。 两个 LongListSele
我在网上看到这个问题准备面试: Given a non-preemptive kernel which type of process will get affected morein terms o
我有一个 foreach 绑定(bind),如下所示: Summary Permitting 原因是有两个选项卡始终存在,并且我根据是否添加了其他选项卡来添加其他选项
任何人都有绑定(bind)相同的情况DataContext到 TextBlock 中的 Text 属性(例如)。 我必须分配 DataContext以我的风格反射(reflect)基于 Datacon
给定以下代码: Login 和下面的javascript $(function () { $('#btnLogin').click(function () { co
我使用 boost::asio 创建了一个服务器。我在绑定(bind)到端点时遇到问题。所以,如果我在构造函数中初始化一个接受器: Server::Server(QWidget *parent) :
我正在将现有项目从 MySQL 转换为 Postgres。代码中有相当多的原始 SQL 文字使用 ? 作为占位符,例如 SELECT id FROM users WHERE
似乎在绑定(bind)某些数据时出错了,有人可以帮我解决我哪里出错了,尽管我无法弄清楚。 真的不需要在这里显示太多,这是 Binding,我已经通过移除背景并在其中放置颜色来测试背景,效果很好。 编辑
我正在尝试使用 wcf 构建一个 http 监听器(web 服务)。这个监听器是一个更大的桌面应用程序的一部分。此桌面应用程序还会调用 http 监听器。 当监听器接收到数据时,它应该被传递到桌面应用
嘿嘿。 我正在使用 Node.JS 和 child_process 来生成 bash 进程。我试图了解我是否正在执行 I/O 绑定(bind)、CPU 绑定(bind)或两者兼而有之。 我正在使用 p
尝试执行以下操作并出现“Got interpolation ({{}}) where expression was expected”错误。 {{item.name}} 谢谢!
我有一个导入的 Java 库,它是我解决方案中的“绑定(bind)库”项目。 我正在尝试从解决方案中的另一个项目绑定(bind)到第 3 方库中的服务。 第 3 方库文档 [在 java 中] 非常简
我是一名优秀的程序员,十分优秀!