Files
EgtCAM5/AttachedBehaviours/AutomaticCloseExpander.vb
T
Emmanuele Sassi 1595b9c2a8 EgtCAM5 :
- Chiusura automatica expander nel DrawPanel e nel OperationParameters Expander.
- Migliorie varie.
2016-09-09 11:39:17 +00:00

152 lines
6.8 KiB
VB.net

'----------------------------------------------------------------------------
' EgalTech 2016-2016
'----------------------------------------------------------------------------
' File : AutomaticCloseExpander.vb Data : 8.9.16 Versione : 1
' Content : AutomatiCloseExpander Behavior (see detailed description below)
'
' Created by : Emmanuele Sassi
' Edited by :
'
' Changes :
'
'----------------------------------------------------------------------------
'
' Behavior description: permit to close other Expander when one is about to expand if they have to use more space than how many the StackPanel offers.
' Control that can use this behavior: StackPanel
' Internal structure of the control that can use it (example):
'
' <StackPanel>
' <Expander Header="Expander"/>
' <Expander Header="Expander"/>
' <Button Height="30" Width="30"/>
' <Expander Header="Expander">
' </Expander>
' <Interactivity:Interaction.Behaviors>
' <local:AutomaticCloseExpander/>
' </Interactivity:Interaction.Behaviors>
' </StackPanel>
'
' IMPORTANT: The behavior have to be added at the end of the StackPanel to function correctly
' Needed library: System.Windows.Interactivity
Imports System.Windows.Interactivity
Public Class AutomaticCloseExpander
Inherits Behavior(Of StackPanel)
Private ExpanderList As New List(Of Expander)
Private OtherControlList As New List(Of FrameworkElement)
Protected Overrides Sub OnAttached()
MyBase.OnAttached()
For Each StackPanelChildren In Me.AssociatedObject.Children
If TypeOf StackPanelChildren Is Expander Then
Dim CurrExpander As Expander = DirectCast(StackPanelChildren, Expander)
ExpanderList.Add(CurrExpander)
AddHandler CurrExpander.Expanded, AddressOf Expander_Expanded
Else
Dim OtherControl As FrameworkElement = DirectCast(StackPanelChildren, FrameworkElement)
OtherControlList.Add(OtherControl)
End If
Next
End Sub
Private Sub Expander_Expanded(sender As Object, e As System.Windows.RoutedEventArgs)
Dim SelectedExpander As Expander = DirectCast(sender, Expander)
If Me.AssociatedObject.Orientation = Orientation.Vertical Then
VerticalVerifyDimension(SelectedExpander)
Else
HorizontalVerifyDimension(SelectedExpander)
End If
End Sub
Private Sub VerticalVerifyDimension(SelectedExpander As Expander)
Dim TotalDimension As Double = 0
'Aggiungo la dimensione di tutti gli Header
For Each Expander In ExpanderList
If Expander IsNot SelectedExpander Then
If Not Expander.IsExpanded Then
TotalDimension += Expander.ActualHeight
Else
Dim ExpanderContent As FrameworkElement = DirectCast(Expander.Content, FrameworkElement)
ExpanderContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
TotalDimension += (Expander.ActualHeight - ExpanderContent.DesiredSize.Height)
End If
Else
TotalDimension += Expander.ActualHeight
End If
Next
' Aggiungo la dimensione dei controlli fissi (non Expander)
For Each OtherControl In OtherControlList
TotalDimension += OtherControl.ActualHeight
Next
' Aggiungo la dimensione del contenuto dell'expander selezionato
Dim SelectedContent As FrameworkElement = DirectCast(SelectedExpander.Content, FrameworkElement)
SelectedContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
TotalDimension += SelectedContent.DesiredSize.Height
' Verifico per tutti gli expander (tranne quello selezionato) a partire dal primo se sono espansi e nel caso se la loro dimensione totale li fa stare nello spazio disponibile
Dim Index As Integer = 0
For Index = 0 To ExpanderList.Count - 1
If ExpanderList(Index) IsNot SelectedExpander Then
If ExpanderList(Index).IsExpanded Then
Dim ExpanderContent As FrameworkElement = DirectCast(ExpanderList(Index).Content, FrameworkElement)
TotalDimension += ExpanderContent.ActualHeight
If TotalDimension > Me.AssociatedObject.MaxHeight Then
Exit For
End If
End If
End If
Next
For CancelIndex = Index To ExpanderList.Count - 1
If ExpanderList(CancelIndex) IsNot SelectedExpander Then
ExpanderList(CancelIndex).IsExpanded = False
End If
Next
End Sub
Private Sub HorizontalVerifyDimension(SelectedExpander As Expander)
Dim TotalDimension As Double = 0
'Aggiungo la dimensione di tutti gli Header
For Each Expander In ExpanderList
If Expander IsNot SelectedExpander Then
If Not Expander.IsExpanded Then
TotalDimension += Expander.ActualWidth
Else
Dim ExpanderContent As FrameworkElement = DirectCast(Expander.Content, FrameworkElement)
ExpanderContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
TotalDimension += (Expander.ActualWidth - ExpanderContent.DesiredSize.Width)
End If
Else
TotalDimension += Expander.ActualWidth
End If
Next
' Aggiungo la dimensione dei controlli fissi (non Expander)
For Each OtherControl In OtherControlList
TotalDimension += OtherControl.ActualWidth
Next
' Aggiungo la dimensione del contenuto dell'expander selezionato
Dim SelectedContent As FrameworkElement = DirectCast(SelectedExpander.Content, FrameworkElement)
SelectedContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity))
TotalDimension += SelectedContent.DesiredSize.Width
' Verifico per tutti gli expander (tranne quello selezionato) a partire dal primo se sono espansi e nel caso se la loro dimensione totale li fa stare nello spazio disponibile
Dim Index As Integer = 0
For Index = 0 To ExpanderList.Count - 1
If ExpanderList(Index) IsNot SelectedExpander Then
If ExpanderList(Index).IsExpanded Then
Dim ExpanderContent As FrameworkElement = DirectCast(ExpanderList(Index).Content, FrameworkElement)
TotalDimension += ExpanderContent.ActualWidth
If TotalDimension > Me.AssociatedObject.ActualWidth Then
Exit For
End If
End If
End If
Next
For CancelIndex = Index To ExpanderList.Count - 1
If ExpanderList(CancelIndex) IsNot SelectedExpander Then
ExpanderList(CancelIndex).IsExpanded = False
End If
Next
End Sub
End Class