fcb88dfe85
- gestite proprieta' HorizontalContentAlignment, VerticalContentAlignment e Padding per bottone principale e DropDown - aggiunto relativo bottone di esempio in DemoApplication
379 lines
13 KiB
VB.net
379 lines
13 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:EgwWPFBaseLib"
|
|
'
|
|
'
|
|
' 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:EgwWPFBaseLib;assembly=EgwWPFBaseLib"
|
|
'
|
|
' 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 cu stom controls and its child elements.
|
|
'
|
|
' <MyNamespace:CustomControl1/>
|
|
'
|
|
|
|
Imports System.Globalization
|
|
Imports System.Windows.Controls.Primitives
|
|
Imports System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar
|
|
|
|
|
|
Public Class EgwSplitButton
|
|
Inherits System.Windows.Controls.ContentControl
|
|
|
|
Public Enum PopupPlacementType
|
|
BottomLeft
|
|
BottomCenter
|
|
BottomRight
|
|
TopLeft
|
|
TopCenter
|
|
TopRight
|
|
LeftTop
|
|
LeftCenter
|
|
LeftBottom
|
|
RightTop
|
|
RightCenter
|
|
RightBottom
|
|
End Enum
|
|
|
|
Public Property DropDownContent As Object
|
|
Get
|
|
Return GetValue(DropDownContentProperty)
|
|
End Get
|
|
Set(value As Object)
|
|
SetValue(DropDownContentProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly DropDownContentProperty As DependencyProperty =
|
|
DependencyProperty.Register("DropDownContent",
|
|
GetType(Object),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(Nothing))
|
|
|
|
' --- Command principale ---
|
|
Public Property Command As ICommand
|
|
Get
|
|
Return CType(GetValue(CommandProperty), ICommand)
|
|
End Get
|
|
Set(value As ICommand)
|
|
SetValue(CommandProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly CommandProperty As DependencyProperty =
|
|
DependencyProperty.Register("Command",
|
|
GetType(ICommand),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(Nothing))
|
|
|
|
' --- Contenuto del pulsante freccia ---
|
|
Public Property DropDownButtonContent As Object
|
|
Get
|
|
Return GetValue(DropDownButtonContentProperty)
|
|
End Get
|
|
Set(value As Object)
|
|
SetValue(DropDownButtonContentProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly DropDownButtonContentProperty As DependencyProperty =
|
|
DependencyProperty.Register("DropDownButtonContent",
|
|
GetType(Object),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata("▼"))
|
|
|
|
' --- Larghezza del pulsante freccia ---
|
|
Public Property DropDownButtonWidth As Double
|
|
Get
|
|
Return CDbl(GetValue(DropDownButtonWidthProperty))
|
|
End Get
|
|
Set(value As Double)
|
|
SetValue(DropDownButtonWidthProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly DropDownButtonWidthProperty As DependencyProperty =
|
|
DependencyProperty.Register("DropDownButtonWidth",
|
|
GetType(Double),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(25.0))
|
|
|
|
' --- Placement del popup ---
|
|
Public Property PopupPlacement As PopupPlacementType
|
|
Get
|
|
Return CType(GetValue(PopupPlacementProperty), PopupPlacementType)
|
|
End Get
|
|
Set(value As PopupPlacementType)
|
|
SetValue(PopupPlacementProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly PopupPlacementProperty As DependencyProperty =
|
|
DependencyProperty.Register("PopupPlacement",
|
|
GetType(PopupPlacementType),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(PopupPlacementType.BottomLeft))
|
|
|
|
Public Property DropDownButtonBackground As Brush
|
|
Get
|
|
Return CType(GetValue(DropDownButtonBackgroundProperty), Brush)
|
|
End Get
|
|
Set(value As Brush)
|
|
SetValue(DropDownButtonBackgroundProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly DropDownButtonBackgroundProperty As DependencyProperty =
|
|
DependencyProperty.Register("DropDownButtonBackground",
|
|
GetType(Brush),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(Nothing)) ' Nothing = usa ButtonBackground
|
|
|
|
Public Property CornerRadius As CornerRadius
|
|
Get
|
|
Return CType(GetValue(CornerRadiusProperty), CornerRadius)
|
|
End Get
|
|
Set(value As CornerRadius)
|
|
SetValue(CornerRadiusProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly CornerRadiusProperty As DependencyProperty =
|
|
DependencyProperty.Register("CornerRadius",
|
|
GetType(CornerRadius),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(New CornerRadius(0)))
|
|
|
|
Public Property SplitBorderBrush As Brush
|
|
Get
|
|
Return CType(GetValue(SplitBorderBrushProperty), Brush)
|
|
End Get
|
|
Set(value As Brush)
|
|
SetValue(SplitBorderBrushProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly SplitBorderBrushProperty As DependencyProperty =
|
|
DependencyProperty.Register("SplitBorderBrush",
|
|
GetType(Brush),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(Brushes.Gray))
|
|
|
|
Public Property SplitBorderThickness As Double
|
|
Get
|
|
Return CType(GetValue(SplitBorderThicknessProperty), Double)
|
|
End Get
|
|
Set(value As Double)
|
|
SetValue(SplitBorderThicknessProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly SplitBorderThicknessProperty As DependencyProperty =
|
|
DependencyProperty.Register("SplitBorderThickness",
|
|
GetType(Double),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(1.0)) ' default: linea verticale
|
|
|
|
Public Property DropDownHorizontalContentAlignment As HorizontalAlignment
|
|
Get
|
|
Return CType(GetValue(DropDownHorizontalContentAlignmentProperty), HorizontalAlignment)
|
|
End Get
|
|
Set(value As HorizontalAlignment)
|
|
SetValue(DropDownHorizontalContentAlignmentProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly DropDownHorizontalContentAlignmentProperty As DependencyProperty =
|
|
DependencyProperty.Register("DropDownHorizontalContentAlignment",
|
|
GetType(HorizontalAlignment),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(HorizontalAlignment.Center))
|
|
|
|
|
|
Public Property DropDownVerticalContentAlignment As VerticalAlignment
|
|
Get
|
|
Return CType(GetValue(DropDownVerticalContentAlignmentProperty), VerticalAlignment)
|
|
End Get
|
|
Set(value As VerticalAlignment)
|
|
SetValue(DropDownVerticalContentAlignmentProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly DropDownVerticalContentAlignmentProperty As DependencyProperty =
|
|
DependencyProperty.Register("DropDownVerticalContentAlignment",
|
|
GetType(VerticalAlignment),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(VerticalAlignment.Center))
|
|
|
|
Public Property DropDownButtonPadding As Thickness
|
|
Get
|
|
Return CType(GetValue(DropDownButtonPaddingProperty), Thickness)
|
|
End Get
|
|
Set(value As Thickness)
|
|
SetValue(DropDownButtonPaddingProperty, value)
|
|
End Set
|
|
End Property
|
|
|
|
Public Shared ReadOnly DropDownButtonPaddingProperty As DependencyProperty =
|
|
DependencyProperty.Register("DropDownButtonPadding",
|
|
GetType(Thickness),
|
|
GetType(EgwSplitButton),
|
|
New PropertyMetadata(New Thickness(4)))
|
|
|
|
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(EgwSplitButton), New FrameworkPropertyMetadata(GetType(EgwSplitButton)))
|
|
End Sub
|
|
|
|
Public Overrides Sub OnApplyTemplate()
|
|
MyBase.OnApplyTemplate()
|
|
|
|
Dim popup = TryCast(GetTemplateChild("PART_Popup"), Popup)
|
|
Dim root = TryCast(Me, FrameworkElement)
|
|
|
|
If popup Is Nothing OrElse root Is Nothing Then Return
|
|
|
|
' PlacementTarget deve essere il controllo, non la freccia
|
|
popup.PlacementTarget = root
|
|
|
|
AddHandler popup.Opened,
|
|
Sub()
|
|
|
|
Dim child = popup.Child
|
|
child.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
|
|
|
|
Dim popupWidth = child.DesiredSize.Width
|
|
Dim popupHeight = child.DesiredSize.Height
|
|
|
|
Dim rootWidth = root.ActualWidth
|
|
Dim rootHeight = root.ActualHeight
|
|
|
|
popup.HorizontalOffset = 0
|
|
popup.VerticalOffset = 0
|
|
|
|
Select Case PopupPlacement
|
|
|
|
' --- BOTTOM ---
|
|
Case PopupPlacementType.BottomLeft
|
|
popup.Placement = PlacementMode.Bottom
|
|
|
|
Case PopupPlacementType.BottomCenter
|
|
popup.Placement = PlacementMode.Bottom
|
|
popup.HorizontalOffset = (rootWidth - popupWidth) / 2
|
|
|
|
Case PopupPlacementType.BottomRight
|
|
popup.Placement = PlacementMode.Bottom
|
|
popup.HorizontalOffset = rootWidth - popupWidth
|
|
|
|
' --- TOP ---
|
|
Case PopupPlacementType.TopLeft
|
|
popup.Placement = PlacementMode.Top
|
|
|
|
Case PopupPlacementType.TopCenter
|
|
popup.Placement = PlacementMode.Top
|
|
popup.HorizontalOffset = (rootWidth - popupWidth) / 2
|
|
|
|
Case PopupPlacementType.TopRight
|
|
popup.Placement = PlacementMode.Top
|
|
popup.HorizontalOffset = rootWidth - popupWidth
|
|
|
|
' --- LEFT ---
|
|
Case PopupPlacementType.LeftTop
|
|
popup.Placement = PlacementMode.Left
|
|
|
|
Case PopupPlacementType.LeftCenter
|
|
popup.Placement = PlacementMode.Left
|
|
popup.VerticalOffset = (rootHeight - popupHeight) / 2
|
|
|
|
Case PopupPlacementType.LeftBottom
|
|
popup.Placement = PlacementMode.Left
|
|
popup.VerticalOffset = rootHeight - popupHeight
|
|
|
|
' --- RIGHT ---
|
|
Case PopupPlacementType.RightTop
|
|
popup.Placement = PlacementMode.Right
|
|
|
|
Case PopupPlacementType.RightCenter
|
|
popup.Placement = PlacementMode.Right
|
|
popup.VerticalOffset = (rootHeight - popupHeight) / 2
|
|
|
|
Case PopupPlacementType.RightBottom
|
|
popup.Placement = PlacementMode.Right
|
|
popup.VerticalOffset = rootHeight - popupHeight
|
|
|
|
End Select
|
|
End Sub
|
|
End Sub
|
|
|
|
End Class
|
|
|
|
Public Class SplitCornerConverter
|
|
Implements IMultiValueConverter
|
|
|
|
Public Enum PartType
|
|
Main
|
|
DropDown
|
|
End Enum
|
|
|
|
Public Property TargetPart As PartType
|
|
|
|
Public Function Convert(values As Object(),
|
|
targetType As Type,
|
|
parameter As Object,
|
|
culture As CultureInfo) As Object _
|
|
Implements IMultiValueConverter.Convert
|
|
|
|
If values Is Nothing OrElse values.Length < 2 Then
|
|
Return New CornerRadius(0)
|
|
End If
|
|
|
|
Dim corner As CornerRadius = CType(values(0), CornerRadius)
|
|
Dim thickness As Thickness = CType(values(1), Thickness)
|
|
|
|
Select Case TargetPart
|
|
Case PartType.Main
|
|
' Arrotonda solo sinistra
|
|
Return New CornerRadius(Math.Max(0, corner.TopLeft - thickness.Left / 2),
|
|
0,
|
|
0,
|
|
Math.Max(0, corner.BottomLeft - thickness.Bottom / 2))
|
|
|
|
Case PartType.DropDown
|
|
' Arrotonda solo destra
|
|
Return New CornerRadius(0,
|
|
Math.Max(0, corner.TopRight - thickness.Top / 2),
|
|
Math.Max(0, corner.BottomRight - thickness.Right / 2),
|
|
0)
|
|
|
|
End Select
|
|
|
|
Return corner
|
|
|
|
End Function
|
|
|
|
Public Function ConvertBack(value As Object,
|
|
targetTypes As Type(),
|
|
parameter As Object,
|
|
culture As CultureInfo) As Object() _
|
|
Implements IMultiValueConverter.ConvertBack
|
|
Throw New NotImplementedException()
|
|
End Function
|
|
|
|
End Class
|