Files
EgtWPFLib5/EgtTextBox.vb
T
Emmanuele Sassi 4ff7cfa57f EgtWPFLib5 1.6s2 :
- Primo rilascio.
2016-07-08 19:56:15 +00:00

91 lines
3.7 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:EgtTextBox/>
'
Imports System.Windows.Controls.Primitives
Imports System.ComponentModel
Public Class EgtTextBox
Inherits System.Windows.Controls.TextBox
' 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 GetValue(ExplicitUpdateSourceProperty)
End Get
Set(ByVal value As UpdateSourceType)
SetValue(ExplicitUpdateSourceProperty, 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 Sub EgtTextBox_PreviewKeyDown(sender As Object, e As System.Windows.Input.KeyEventArgs) Handles Me.PreviewKeyDown
If e.Key = Key.Enter Then
If ExplicitUpdateSource = UpdateSourceType.EnterKeyPress Then
Me.GetBindingExpression(TextBox.TextProperty).UpdateSource()
End If
Keyboard.ClearFocus()
End If
End Sub
Private Sub EgtTextBox_TextChanged(sender As Object, e As System.Windows.Controls.TextChangedEventArgs) Handles Me.TextChanged
If ExplicitUpdateSource = UpdateSourceType.PropertyChanged Then
Me.GetBindingExpression(TextBox.TextProperty).UpdateSource()
End If
End Sub
Private Sub ExplicitBindingTextBox_Loaded(sender As Object, e As RoutedEventArgs)
Dim textBox As TextBox = TryCast(sender, TextBox)
If TryCast(textBox.DataContext, INotifyPropertyChanged) Is Nothing Then
Throw New InvalidOperationException("...")
End If
AddHandler TryCast(textBox.DataContext, INotifyPropertyChanged).PropertyChanged, AddressOf ViewModel_PropertyChanged
End Sub
Private Sub ViewModel_PropertyChanged(sender As Object, e As System.ComponentModel.PropertyChangedEventArgs)
If ExplicitUpdateSource = UpdateSourceType.PropertyChanged Or ExplicitUpdateSource = UpdateSourceType.EnterKeyPress Then
Me.GetBindingExpression(TextBox.TextProperty).UpdateTarget()
End If
End Sub
End Class