Files
EgtCAM5/AttachedBehaviours/InputBindingsManager.vb
Emmanuele Sassi 6233d89e0f EgtCAM5 :
- primo rilascio.
2016-07-06 09:46:23 +00:00

61 lines
2.2 KiB
VB.net

Public NotInheritable Class InputBindingsManager
Private Sub New()
End Sub
Public Shared ReadOnly UpdatePropertySourceWhenEnterPressedProperty As DependencyProperty = DependencyProperty.RegisterAttached("UpdatePropertySourceWhenEnterPressed", GetType(DependencyProperty), GetType(InputBindingsManager), New PropertyMetadata(Nothing, AddressOf OnUpdatePropertySourceWhenEnterPressedPropertyChanged))
Shared Sub New()
End Sub
Public Shared Sub SetUpdatePropertySourceWhenEnterPressed(dp As DependencyObject, value As DependencyProperty)
dp.SetValue(UpdatePropertySourceWhenEnterPressedProperty, value)
End Sub
Public Shared Function GetUpdatePropertySourceWhenEnterPressed(dp As DependencyObject) As DependencyProperty
Return DirectCast(dp.GetValue(UpdatePropertySourceWhenEnterPressedProperty), DependencyProperty)
End Function
Private Shared Sub OnUpdatePropertySourceWhenEnterPressedPropertyChanged(dp As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim element As UIElement = TryCast(dp, UIElement)
If element Is Nothing Then
Return
End If
If e.OldValue IsNot Nothing Then
RemoveHandler element.PreviewKeyDown, AddressOf HandlePreviewKeyDown
End If
If e.NewValue IsNot Nothing Then
AddHandler element.PreviewKeyDown, New KeyEventHandler(AddressOf HandlePreviewKeyDown)
End If
End Sub
Private Shared Sub HandlePreviewKeyDown(sender As Object, e As KeyEventArgs)
If e.Key = Key.Enter Then
DoUpdateSource(e.Source)
End If
End Sub
Private Shared Sub DoUpdateSource(source As Object)
Dim [property] As DependencyProperty = GetUpdatePropertySourceWhenEnterPressed(TryCast(source, DependencyObject))
If [property] Is Nothing Then
Return
End If
Dim elt As UIElement = TryCast(source, UIElement)
If elt Is Nothing Then
Return
End If
Dim binding As BindingExpression = BindingOperations.GetBindingExpression(elt, [property])
If binding IsNot Nothing Then
binding.UpdateSource()
End If
End Sub
End Class