Files
EgtWPFLib5/EgtTextBox2.vb
2025-03-21 23:42:17 +01:00

390 lines
17 KiB
VB.net

' Follow steps 1a or 1b and then 2 to use this custom control in a XAML file.
'
' Step 1a) Using this custom control in a XAML file that exists in the current project.
' Add this XmlNamespace attribute to the root element of the markup file where it is
' to be used:
'
' xmlns:MyNamespace="clr-namespace:EgtWPFLib5"
'
'
' Step 1b) Using this custom control in a XAML file that exists in a different project.
' Add this XmlNamespace attribute to the root element of the markup file where it is
' to be used:
'
' xmlns:MyNamespace="clr-namespace:EgtWPFLib5;assembly=EgtWPFLib5"
'
' You will also need to add a project reference from the project where the XAML file lives
' to this project and Rebuild to avoid compilation errors:
'
' Right click on the target project in the Solution Explorer and
' "Add Reference"->"Projects"->[Browse to and select this project]
'
'
' Step 2)
' Go ahead and use your control in the XAML file. Note that Intellisense in the
' XML editor does not currently work on custom controls and its child elements.
'
' <MyNamespace:EgtTextBox2/>
'
<TemplatePart(Name:="PART_TextBox", Type:=GetType(TextBox))>
Public Class EgtTextBox2
Inherits System.Windows.Controls.Control
#Region "FIELDS & PROPERTIES"
Public Enum UpdateSourceType As Integer
Null = 0
PropertyChanged = 1
EnterKeyPress = 2
EnterKeyPressOrLostFocus = 3
End Enum
' TextBox creata nello style di questo elemento
Private WithEvents m_MyTextBox As TextBox
Public ReadOnly Property MyTextBox As TextBox
Get
Return m_MyTextBox
End Get
End Property
#Region "TextBox Parameters"
Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(EgtTextBox2), New FrameworkPropertyMetadata(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
Public Shared ReadOnly AcceptsReturnProperty As DependencyProperty = DependencyProperty.Register("AcceptsReturn", GetType(Boolean), GetType(EgtTextBox2), New FrameworkPropertyMetadata(False))
Public Property AcceptsReturn As Boolean
Get
Return CBool(GetValue(AcceptsReturnProperty))
End Get
Set(value As Boolean)
SetValue(AcceptsReturnProperty, value)
End Set
End Property
Public Shared ReadOnly AcceptsTabProperty As DependencyProperty = DependencyProperty.Register("AcceptsTab", GetType(Boolean), GetType(EgtTextBox2), New FrameworkPropertyMetadata(False))
Public Property AcceptsTab As Boolean
Get
Return CBool(GetValue(AcceptsTabProperty))
End Get
Set(value As Boolean)
SetValue(AcceptsTabProperty, value)
End Set
End Property
Public Shared ReadOnly CaretBrushProperty As DependencyProperty = DependencyProperty.Register("CaretBrush", GetType(Brush), GetType(EgtTextBox2), New FrameworkPropertyMetadata(Brushes.Black))
Public Property CaretBrush As Brush
Get
Return DirectCast(GetValue(CaretBrushProperty), Brush)
End Get
Set(value As Brush)
SetValue(CaretBrushProperty, value)
End Set
End Property
Public Property CaretIndex As Integer
Get
Return m_MyTextBox.CaretIndex
End Get
Set(value As Integer)
m_MyTextBox.CaretIndex = value
End Set
End Property
Public Property SelectedText As String
Get
Return m_MyTextBox.SelectedText
End Get
Set(value As String)
m_MyTextBox.SelectedText = value
End Set
End Property
Public Property SelectionLength As Integer
Get
Return m_MyTextBox.SelectionLength
End Get
Set(value As Integer)
m_MyTextBox.SelectionLength = value
End Set
End Property
Public Property SelectionStart As Integer
Get
Return m_MyTextBox.SelectionStart
End Get
Set(value As Integer)
m_MyTextBox.SelectionStart = value
End Set
End Property
Public Shared ReadOnly CharacterCasingProperty As DependencyProperty = DependencyProperty.Register("CharacterCasing", GetType(CharacterCasing), GetType(EgtTextBox2), New FrameworkPropertyMetadata(CharacterCasing.Normal))
Public Property CharacterCasing As CharacterCasing
Get
Return DirectCast(GetValue(CharacterCasingProperty), CharacterCasing)
End Get
Set(value As CharacterCasing)
SetValue(CharacterCasingProperty, value)
End Set
End Property
Public Shared ReadOnly HorizontalScrollBarVisibilityProperty As DependencyProperty = DependencyProperty.Register("HorizontalScrollBarVisibility", GetType(ScrollBarVisibility), GetType(EgtTextBox2), New FrameworkPropertyMetadata(ScrollBarVisibility.Hidden))
Public Property HorizontalScrollBarVisibility As ScrollBarVisibility
Get
Return DirectCast(GetValue(HorizontalScrollBarVisibilityProperty), ScrollBarVisibility)
End Get
Set(value As ScrollBarVisibility)
SetValue(HorizontalScrollBarVisibilityProperty, value)
End Set
End Property
Public Shared ReadOnly IsReadOnlyProperty As DependencyProperty = DependencyProperty.Register("IsReadOnly", GetType(Boolean), GetType(EgtTextBox2), New FrameworkPropertyMetadata(False, New PropertyChangedCallback(AddressOf IsReadOnly_PropertyChanged)))
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 ReadOnly IsReadOnlyCaretVisibleProperty As DependencyProperty = DependencyProperty.Register("IsReadOnlyCaretVisible", GetType(Boolean), GetType(EgtTextBox2), New FrameworkPropertyMetadata(False))
Public Property IsReadOnlyCaretVisible As Boolean
Get
Return CBool(GetValue(IsReadOnlyCaretVisibleProperty))
End Get
Set(value As Boolean)
SetValue(IsReadOnlyCaretVisibleProperty, value)
End Set
End Property
Public Shared ReadOnly IsUndoEnabledProperty As DependencyProperty = DependencyProperty.Register("IsUndoEnabled", GetType(Boolean), GetType(EgtTextBox2), New FrameworkPropertyMetadata(True))
Public Property IsUndoEnabled As Boolean
Get
Return CBool(GetValue(IsUndoEnabledProperty))
End Get
Set(value As Boolean)
SetValue(IsUndoEnabledProperty, value)
End Set
End Property
Public Shared ReadOnly MaxLengthProperty As DependencyProperty = DependencyProperty.Register("MaxLength", GetType(Integer), GetType(EgtTextBox2), New FrameworkPropertyMetadata())
Public Property MaxLength As Integer
Get
Return CInt(GetValue(MaxLengthProperty))
End Get
Set(value As Integer)
SetValue(MaxLengthProperty, value)
End Set
End Property
Public Shared ReadOnly MaxLinesProperty As DependencyProperty = DependencyProperty.Register("MaxLines", GetType(Integer), GetType(EgtTextBox2), New FrameworkPropertyMetadata(Integer.MaxValue))
Public Property MaxLines As Integer
Get
Return CInt(GetValue(MaxLinesProperty))
End Get
Set(value As Integer)
SetValue(MaxLinesProperty, value)
End Set
End Property
Public Shared ReadOnly MinLinesProperty As DependencyProperty = DependencyProperty.Register("MinLines", GetType(Integer), GetType(EgtTextBox2), New FrameworkPropertyMetadata(1))
Public Property MinLines As Integer
Get
Return CInt(GetValue(MinLinesProperty))
End Get
Set(value As Integer)
SetValue(MinLinesProperty, value)
End Set
End Property
Public Shared Shadows ReadOnly PaddingProperty As DependencyProperty = DependencyProperty.Register("Padding", GetType(Thickness), GetType(EgtTextBox2), New FrameworkPropertyMetadata(New Thickness()))
Public Shadows Property Padding As Integer
Get
Return CInt(GetValue(PaddingProperty))
End Get
Set(value As Integer)
SetValue(PaddingProperty, value)
End Set
End Property
Public Shared ReadOnly RenderSizeProperty As DependencyProperty = DependencyProperty.Register("RenderSize", GetType(Size), GetType(EgtTextBox2), New FrameworkPropertyMetadata())
Public Shared ReadOnly SelectionBrushProperty As DependencyProperty = DependencyProperty.Register("SelectionBrush", GetType(Brush), GetType(EgtTextBox2), New FrameworkPropertyMetadata(New BrushConverter().ConvertFromString("#0078D7")))
Public Property SelectionBrush As Brush
Get
Return DirectCast(GetValue(SelectionBrushProperty), Brush)
End Get
Set(value As Brush)
SetValue(SelectionBrushProperty, value)
End Set
End Property
Public Shared ReadOnly SelectionOpacityProperty As DependencyProperty = DependencyProperty.Register("SelectionOpacity", GetType(Double), GetType(EgtTextBox2), New FrameworkPropertyMetadata(0.4))
Public Property SelectionOpacity As Double
Get
Return CDbl(GetValue(SelectionOpacityProperty))
End Get
Set(value As Double)
SetValue(SelectionOpacityProperty, value)
End Set
End Property
Public Shared ReadOnly TextAlignmentProperty As DependencyProperty = DependencyProperty.Register("TextAlignment", GetType(TextAlignment), GetType(EgtTextBox2), New FrameworkPropertyMetadata(TextAlignment.Left))
Public Property TextAlignment As TextAlignment
Get
Return DirectCast(GetValue(TextAlignmentProperty), TextAlignment)
End Get
Set(value As TextAlignment)
SetValue(TextAlignmentProperty, value)
End Set
End Property
Public Shared ReadOnly TextDecorationsProperty As DependencyProperty = DependencyProperty.Register("TextDecorations", GetType(TextDecorationCollection), GetType(EgtTextBox2), New FrameworkPropertyMetadata(Nothing))
Public Property TextDecorations As TextDecorationCollection
Get
Return DirectCast(GetValue(TextDecorationsProperty), TextDecorationCollection)
End Get
Set(value As TextDecorationCollection)
SetValue(TextDecorationsProperty, value)
End Set
End Property
Public Shared ReadOnly TextWrappingProperty As DependencyProperty = DependencyProperty.Register("TextWrapping", GetType(TextWrapping), GetType(EgtTextBox2), New FrameworkPropertyMetadata(TextWrapping.NoWrap))
Public Property TextWrapping As TextWrapping
Get
Return DirectCast(GetValue(TextWrappingProperty), TextWrapping)
End Get
Set(value As TextWrapping)
SetValue(TextWrappingProperty, value)
End Set
End Property
Public Shared ReadOnly UndoLimitProperty As DependencyProperty = DependencyProperty.Register("UndoLimit", GetType(Integer), GetType(EgtTextBox2), New FrameworkPropertyMetadata(-1))
Public Property UndoLimit As Integer
Get
Return CInt(GetValue(UndoLimitProperty))
End Get
Set(value As Integer)
SetValue(UndoLimitProperty, value)
End Set
End Property
Public Shared ReadOnly VerticalScrollBarVisibilityProperty As DependencyProperty = DependencyProperty.Register("VerticalScrollBarVisibility", GetType(ScrollBarVisibility), GetType(EgtTextBox2), New FrameworkPropertyMetadata(ScrollBarVisibility.Hidden))
Public Property VerticalScrollBarVisibility As ScrollBarVisibility
Get
Return DirectCast(GetValue(VerticalScrollBarVisibilityProperty), ScrollBarVisibility)
End Get
Set(value As ScrollBarVisibility)
SetValue(VerticalScrollBarVisibilityProperty, value)
End Set
End Property
#End Region ' TextBox Parameters
' Proprietà che permette di impostare il tipo di binding del testo
Public Shared ReadOnly ExplicitUpdateSourceProperty As DependencyProperty = DependencyProperty.Register("ExplicitUpdateSource", GetType(UpdateSourceType), GetType(EgtTextBox2), 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
' Proprietà che permette di impostare quando resettare il valore
Public Shared ReadOnly ResetValueOnLostFocusProperty As DependencyProperty = DependencyProperty.Register("ResetValueOnLostFocus", GetType(Boolean), GetType(EgtTextBox2), New PropertyMetadata(False))
Public Property ResetValueOnLostFocus() As Boolean
Get
Return CBool(GetValue(ResetValueOnLostFocusProperty))
End Get
Set(ByVal value As Boolean)
SetValue(ResetValueOnLostFocusProperty, value)
End Set
End Property
Private m_bActivateDuringCreation_IsExplicitFocused As Boolean = False
' Proprietà che permette di impostare il il focus sulla textbox
Public Shared ReadOnly IsExplicitFocusedProperty As DependencyProperty = DependencyProperty.Register("IsExplicitFocused", GetType(Boolean), GetType(EgtTextBox2), New UIPropertyMetadata(False, AddressOf OnIsFocusedPropertyChanged))
Public Property IsExplicitFocused() As Boolean
Get
Return CBool(GetValue(IsExplicitFocusedProperty))
End Get
Set(ByVal value As Boolean)
SetValue(IsExplicitFocusedProperty, value)
End Set
End Property
#End Region ' FIELDS & PROPERTIES
#Region "CONSTRUCTORS"
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(EgtTextBox2), New FrameworkPropertyMetadata(GetType(EgtTextBox2)))
End Sub
Sub New()
MyBase.New()
End Sub
#End Region ' CONSTRUCTORS
#Region "METHODS"
Public Overrides Sub OnApplyTemplate()
MyBase.OnApplyTemplate()
m_MyTextBox = TryCast(MyBase.GetTemplateChild("PART_TextBox"), TextBox)
m_MyTextBox.Text = Text
If m_bActivateDuringCreation_IsExplicitFocused Then
m_MyTextBox.Focus()
End If
End Sub
Private Shared Sub IsReadOnly_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim Temp As EgtTextBox2 = DirectCast(sender, EgtTextBox2)
If Not IsNothing(Temp.MyTextBox) Then Temp.MyTextBox.IsReadOnly = CBool(e.NewValue)
End Sub
' evento di modifica della proprietà Text della EgtTextBox2
Private Shared Sub Text_PropertyChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim Temp As EgtTextBox2 = DirectCast(sender, EgtTextBox2)
If Not IsNothing(Temp.m_MyTextBox) Then Temp.m_MyTextBox.Text = CStr(e.NewValue)
End Sub
Private Shared Sub OnIsFocusedPropertyChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
If CBool(e.NewValue) Then
Dim Temp As EgtTextBox2 = TryCast(d, EgtTextBox2)
If IsNothing(Temp) OrElse IsNothing(Temp.m_MyTextBox) Then
Temp.m_bActivateDuringCreation_IsExplicitFocused = True
ElseIf Not IsNothing(Temp) AndAlso Not IsNothing(Temp.m_MyTextBox) AndAlso Not Temp.IsReadOnly Then
Temp.m_MyTextBox.Focus()
End If
End If
End Sub
Sub MyTextBox_LostKeyboardFocus(sender As Object, e As KeyboardFocusChangedEventArgs) Handles m_MyTextBox.LostKeyboardFocus
If Not Me.IsReadOnly Then
If ExplicitUpdateSource = UpdateSourceType.EnterKeyPressOrLostFocus Then
Me.GetBindingExpression(EgtTextBox2.TextProperty).UpdateSource()
ElseIf ResetValueOnLostFocus Then
Me.GetBindingExpression(EgtTextBox2.TextProperty).UpdateTarget()
End If
End If
m_MyTextBox.SelectionStart = 0
m_MyTextBox.SelectionLength = 0
End Sub
Sub MyTextBox_LostFocus(sender As Object, e As RoutedEventArgs) Handles m_MyTextBox.LostFocus
m_MyTextBox.SelectionStart = 0
m_MyTextBox.SelectionLength = 0
End Sub
Private Sub EgtTextBox2_PreviewKeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs) Handles Me.PreviewKeyDown
If (e.Key = Key.Enter OrElse e.Key = Key.Escape) AndAlso Not Me.IsReadOnly Then
If ExplicitUpdateSource = UpdateSourceType.EnterKeyPress OrElse ExplicitUpdateSource = UpdateSourceType.EnterKeyPressOrLostFocus Then
Me.GetBindingExpression(EgtTextBox2.TextProperty).UpdateSource()
End If
Keyboard.ClearFocus()
End If
End Sub
' evento di modifica della proprietà Text della TextBox della EgtTextBox2
Private Sub MyTextBox_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles m_MyTextBox.TextChanged
Text = m_MyTextBox.Text
If ExplicitUpdateSource = UpdateSourceType.PropertyChanged Then
Me.GetBindingExpression(EgtTextBox2.TextProperty).UpdateSource()
End If
End Sub
#End Region ' METHODS
End Class