Files
EgtWPFLib5/EgtTextBox.xaml.vb
T
2022-09-12 15:38:28 +02:00

293 lines
13 KiB
VB.net

Imports System.ComponentModel
Public Class EgtTextBox
Sub New()
InitializeComponent()
End Sub
Public Shared ReadOnly TextProperty As DependencyProperty =
DependencyProperty.Register("Text", GetType(String), GetType(EgtTextBox), New FrameworkPropertyMetadata(String.Empty, New PropertyChangedCallback(AddressOf Text_PropertyChanged)) With {.BindsTwoWayByDefault = True})
Public Property Text As String
Get
Return CStr(GetValue(TextProperty))
End Get
Set(value As String)
SetValue(TextProperty, value)
End Set
End Property
' Delegato ed Evento creati per gestire il TextChanged nel code-behind di LicenseManager
Public Delegate Sub EgtTextBoxTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
Public Event TextChanged As EgtTextBoxTextChanged
Protected Sub EgtTextBox_TextChanged_LM(sender As Object, e As TextChangedEventArgs) Handles TxBx.TextChanged
RaiseEvent TextChanged(sender, e)
End Sub
Public ReadOnly Property LineCount As Integer
Get
Return TxBx.LineCount
End Get
End Property
Public Shared ReadOnly IsReadOnlyProperty As DependencyProperty =
DependencyProperty.Register("IsReadOnly", GetType(Boolean), GetType(EgtTextBox), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf IsReadOnly_PropertyChanged)) With {.BindsTwoWayByDefault = True})
Public Property IsReadOnly As Boolean
Get
Return CBool(GetValue(IsReadOnlyProperty))
End Get
Set(value As Boolean)
SetValue(IsReadOnlyProperty, value)
End Set
End Property
Public Shared Shadows ReadOnly VerticalContentAlignmentProperty As DependencyProperty =
DependencyProperty.Register("VerticalContentAlignment", GetType(VerticalAlignment), GetType(EgtTextBox), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf VerticalAlignment_PropertyChanged)) With {.BindsTwoWayByDefault = True})
Public Overloads Property VerticalContentAlignment As VerticalAlignment
Get
Return CType(GetValue(VerticalContentAlignmentProperty), VerticalAlignment)
End Get
Set(value As VerticalAlignment)
SetValue(VerticalContentAlignmentProperty, value)
End Set
End Property
Public Shared Shadows ReadOnly HorizontalContentAlignmentProperty As DependencyProperty =
DependencyProperty.Register("HorizontalContentAlignment", GetType(TextAlignment), GetType(EgtTextBox), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf TextAlignment_PropertyChanged)) With {.BindsTwoWayByDefault = True})
Public Overloads Property HorizontalContentAlignment As TextAlignment
Get
Return CType(GetValue(HorizontalContentAlignmentProperty), TextAlignment)
End Get
Set(value As TextAlignment)
SetValue(HorizontalContentAlignmentProperty, value)
End Set
End Property
Public Shared ReadOnly TextAlignmentProperty As DependencyProperty =
DependencyProperty.Register("TextAlignment", GetType(TextAlignment), GetType(EgtTextBox), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf TextAlignment_PropertyChanged)) With {.BindsTwoWayByDefault = True})
Public Property TextAlignment As TextAlignment
Get
Return CType(GetValue(TextAlignmentProperty), TextAlignment)
End Get
Set(value As TextAlignment)
SetValue(TextAlignmentProperty, value)
End Set
End Property
Public Shared ReadOnly TextWrappingProperty As DependencyProperty =
DependencyProperty.Register("TextWrapping", GetType(TextWrapping), GetType(EgtTextBox), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf TextWrapping_PropertyChanged)) With {.BindsTwoWayByDefault = True})
Public Property TextWrapping As TextWrapping
Get
Return CType(GetValue(TextWrappingProperty), TextWrapping)
End Get
Set(value As TextWrapping)
SetValue(TextWrappingProperty, value)
End Set
End Property
Public Shared ReadOnly TextCharacterCasingProperty As DependencyProperty =
DependencyProperty.Register("CharacterCasing", GetType(CharacterCasing), GetType(EgtTextBox), New FrameworkPropertyMetadata(New PropertyChangedCallback(AddressOf TextWrapping_PropertyChanged)))
Public Property CharacterCasing As CharacterCasing
Get
Return CType(GetValue(TextCharacterCasingProperty), CharacterCasing)
End Get
Set(value As CharacterCasing)
SetValue(TextCharacterCasingProperty, value)
End Set
End Property
Private Shared Sub Text_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim TempMe As EgtTextBox = DirectCast(sender, EgtTextBox)
If TempMe.ActiveTextBox Then
TempMe.TxBx.Text = CStr(e.NewValue)
Else
TempMe.TxBl.Text = CStr(e.NewValue)
End If
End Sub
Private Shared Sub TextAlignment_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim TempMe As EgtTextBox = DirectCast(sender, EgtTextBox)
TempMe.TxBl.TextAlignment = CType(e.NewValue, TextAlignment)
TempMe.TxBx.TextAlignment = CType(e.NewValue, TextAlignment)
End Sub
Private Shared Sub VerticalAlignment_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim TempMe As EgtTextBox = DirectCast(sender, EgtTextBox)
TempMe.TxBl.VerticalAlignment = CType(e.NewValue, VerticalAlignment)
TempMe.TxBx.VerticalContentAlignment = CType(e.NewValue, VerticalAlignment)
' Il Padding di TxBx è settato a "1,1,1,0" ma quando VerticalContentAlignment è "Center" o "Bottom" occorre incrementare il Bottom
' del Padding a 1 settandolo dunque a "1,1,1,1". In questo modo l'allineamento del testo tra TxBl e TxBx è garantito.
If TempMe.TxBx.VerticalContentAlignment = VerticalAlignment.Center Or TempMe.TxBx.VerticalContentAlignment = VerticalAlignment.Bottom Then
TempMe.TxBx.Padding = New Thickness(1, 1, 1, 1)
End If
End Sub
Private Shared Sub IsReadOnly_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim TempMe As EgtTextBox = DirectCast(sender, EgtTextBox)
TempMe.TxBx.IsReadOnly = CBool(e.NewValue)
End Sub
Private Shared Sub TextWrapping_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim TempMe As EgtTextBox = DirectCast(sender, EgtTextBox)
TempMe.TxBx.TextWrapping = CType(e.NewValue, TextWrapping)
TempMe.TxBl.TextWrapping = CType(e.NewValue, TextWrapping)
End Sub
Private Shared Sub TextCharacterCasing_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim TempMe As EgtTextBox = DirectCast(sender, EgtTextBox)
TempMe.TxBx.CharacterCasing = CType(e.NewValue, CharacterCasing)
' TempMe.TxBl.CharacterCasing = CType(e.NewValue, CharacterCasing)
End Sub
Private Sub TxBx_TextChanged(sender As Object, e As TextChangedEventArgs) Handles TxBx.TextChanged
Text = TxBx.Text
End Sub
'Used to track whether Or Not this TextBox is active.
Private ActiveTextBox As Boolean = False
' Use to track if some exiting event is fired
Private m_bSuspendLostKeyboardFocus As Boolean = False
Private m_bSuspendLostFocus As Boolean = False
'Activates the hit TextBox if it isn't active.
'Deactivates the active TextBox if something else was hit.
Sub Me_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.MouseDown
If Not ActiveTextBox Then
Activate()
Dim eMouseDown As New MouseButtonEventArgs(e.MouseDevice, e.Timestamp, e.ChangedButton, e.StylusDevice)
eMouseDown.RoutedEvent = e.RoutedEvent
eMouseDown.Source = TxBx
TxBx.RaiseEvent(eMouseDown)
End If
End Sub
'Deactivates the active TextBox if the Enter Or ESC key Is pressed.
Sub HandleKeyInput(sender As Object, args As KeyEventArgs)
If ActiveTextBox And (args.Key = Key.Return Or args.Key = Key.Escape) Then Deactivate()
End Sub
'Deactivate the Textbox if it's active.
Sub HandleLostFocus(sender As Object, e As RoutedEventArgs)
If ActiveTextBox AndAlso Not m_bSuspendLostFocus Then
m_bSuspendLostFocus = True
Deactivate()
m_bSuspendLostFocus = False
End If
End Sub
Sub HandleLostKeyboardFocus(sender As Object, e As KeyboardFocusChangedEventArgs)
If ActiveTextBox AndAlso Not m_bSuspendLostKeyboardFocus AndAlso Not m_bSuspendLostFocus Then
m_bSuspendLostFocus = True
If Not Me.IsReadOnly Then
If ExplicitUpdateSource = UpdateSourceType.EnterKeyPress Then
Me.GetBindingExpression(EgtTextBox.TextProperty).UpdateSource()
End If
End If
Deactivate()
m_bSuspendLostFocus = False
End If
End Sub
'Activate the TextBox by changing Visibility and copying text from the TextBlock to the TextBox.
'Set focus, Select all the content & invalidate the visual.
'Also, dynamically add handlers for losing focus And handling key input.
Sub Activate()
If ActiveTextBox Then Return
Brd.Visibility = Visibility.Hidden
TxBl.Visibility = Visibility.Hidden
TxBx.Visibility = Visibility.Visible
TxBx.Text = TxBl.Text
Me.TxBx.Focus()
Me.TxBx.InvalidateVisual()
AddHandler Me.LostFocus, New RoutedEventHandler(AddressOf HandleLostFocus)
AddHandler Me.LostKeyboardFocus, New KeyboardFocusChangedEventHandler(AddressOf HandleLostKeyboardFocus)
AddHandler Me.KeyDown, New KeyEventHandler(AddressOf HandleKeyInput)
ActiveTextBox = True
End Sub
'Deactivate the TextBox by changing Visibility, deselecting the selected text and copying text from the TextBox to the TextBlock.
'Remove the event handlers.
Sub Deactivate()
If Not ActiveTextBox Then Return
Brd.Visibility = Visibility.Visible
TxBl.Visibility = Visibility.Visible
TxBx.SelectionLength = 0
TxBx.Visibility = Visibility.Hidden
TxBl.Text = TxBx.Text
RemoveHandler Me.LostFocus, New RoutedEventHandler(AddressOf HandleLostFocus)
RemoveHandler Me.LostKeyboardFocus, New KeyboardFocusChangedEventHandler(AddressOf HandleLostKeyboardFocus)
RemoveHandler Me.KeyDown, New KeyEventHandler(AddressOf HandleKeyInput)
ActiveTextBox = False
End Sub
#Region "EgtTextBox originale"
' Proprietà che permette di impostare l'altezza della TitleBar
Public Shared ReadOnly ExplicitUpdateSourceProperty As DependencyProperty = DependencyProperty.Register("ExplicitUpdateSource", GetType(UpdateSourceType), GetType(EgtTextBox), New PropertyMetadata(UpdateSourceType.Null))
Public Property ExplicitUpdateSource() As UpdateSourceType
Get
Return DirectCast(GetValue(ExplicitUpdateSourceProperty), UpdateSourceType)
End Get
Set(ByVal value As UpdateSourceType)
SetValue(ExplicitUpdateSourceProperty, value)
End Set
End Property
Public Shared ReadOnly IsExplicitFocusedProperty As DependencyProperty = DependencyProperty.Register("IsExplicitFocused", GetType(Boolean), GetType(EgtTextBox), New UIPropertyMetadata(False, AddressOf OnIsFocusedPropertyChanged))
Public Property IsExplicitFocused() As Boolean
Get
Return CBool(GetValue(IsExplicitFocusedProperty))
End Get
Set(ByVal value As Boolean)
Me.Focus()
SetValue(IsExplicitFocusedProperty, value)
End Set
End Property
Public Enum UpdateSourceType As Integer
Null = 0
PropertyChanged = 1
EnterKeyPress = 2
End Enum
'Shared Sub New()
' 'This OverrideMetadata call tells the system that this element wants to provide a style that is different than its base class.
' 'This style is defined in themes\generic.xaml
' DefaultStyleKeyProperty.OverrideMetadata(GetType(EgtTextBox), New FrameworkPropertyMetadata(GetType(EgtTextBox)))
'End Sub
Private Shared Sub OnIsFocusedPropertyChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim uie = DirectCast(d, UIElement)
uie.Focus()
End Sub
Private Sub EgtTextBox_PreviewKeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs) Handles Me.PreviewKeyDown
If e.Key = Key.Enter AndAlso Not Me.IsReadOnly Then
m_bSuspendLostKeyboardFocus = True
If ExplicitUpdateSource = UpdateSourceType.EnterKeyPress Then
Me.GetBindingExpression(EgtTextBox.TextProperty).UpdateSource()
End If
Keyboard.ClearFocus()
Deactivate()
m_bSuspendLostKeyboardFocus = False
End If
End Sub
Private Sub EgtTextBox_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles TxBx.TextChanged 'Me.TextChanged
If ExplicitUpdateSource = UpdateSourceType.PropertyChanged Then
Me.GetBindingExpression(EgtTextBox.TextProperty).UpdateSource()
End If
End Sub
#End Region
End Class