Imports EgtWPFLib5.EgtFloating Public Class ProbingParameterExpanderV Private m_RightTrayV As RightTrayV Private EgtFloatingPanel As EgtFloatingPanel Private m_bFirst As Boolean = True Private Sub OperationExpanderView_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded If m_bFirst Then m_RightTrayV = FindAncestor(Of RightTrayV)(Me) EgtFloatingPanel = FindAncestor(Of EgtFloatingPanel)(Me) AddHandler m_RightTrayV.SizeChanged, AddressOf RightTrayV_SizeChanged PanelHeight = EgtFloatingPanel.DesiredSize.Height Me.AutomaticCloseExpanderStackPanelHeight = AutomaticCloseExpanderStackPanel.ActualHeight m_bFirst = False End If CalculateOperationParametersStackPanelMaxHeight() End Sub Dim m_bSizeChanging As Boolean = False Private Sub RightTrayV_SizeChanged(sender As Object, e As System.Windows.SizeChangedEventArgs) If m_bSizeChanging Then Return m_bSizeChanging = True CalculateOperationParametersStackPanelMaxHeight() m_bSizeChanging = False End Sub Dim PanelHeight As Double = 0 Dim AutomaticCloseExpanderStackPanelHeight As Double = 0 ' ricalcola spazio utilizzabile quando modificata dimensione finestra e quindi dimensione della tray Private Sub CalculateOperationParametersStackPanelMaxHeight() Dim AutomaticCloseExpanderStackPanelHeight As Double = 0 Dim OpenedExpanderList As New List(Of Boolean) For Index = 0 To AutomaticCloseExpanderStackPanel.Children.Count - 1 If TypeOf AutomaticCloseExpanderStackPanel.Children(Index) Is Expander Then Dim IndexedExpander As Expander = DirectCast(AutomaticCloseExpanderStackPanel.Children(Index), Expander) If IndexedExpander.IsExpanded Then OpenedExpanderList.Add(True) IndexedExpander.IsExpanded = False Dim CurrExpanderContent As FrameworkElement = DirectCast(IndexedExpander.Content, FrameworkElement) CurrExpanderContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity)) AutomaticCloseExpanderStackPanelHeight -= CurrExpanderContent.DesiredSize.Height ' Sottraggo anche i padding AutomaticCloseExpanderStackPanelHeight -= (IndexedExpander.Padding.Top + IndexedExpander.Padding.Bottom) Else OpenedExpanderList.Add(False) End If Else OpenedExpanderList.Add(False) End If Next Dim RemainingHeight As Double = m_RightTrayV.ActualHeight - PanelHeight AutomaticCloseExpanderStackPanel.MaxHeight = Me.AutomaticCloseExpanderStackPanelHeight + RemainingHeight For Index = AutomaticCloseExpanderStackPanel.Children.Count - 1 To 0 Step -1 If TypeOf AutomaticCloseExpanderStackPanel.Children(Index) Is Expander Then Dim IndexedExpander As Expander = DirectCast(AutomaticCloseExpanderStackPanel.Children(Index), Expander) If OpenedExpanderList(Index) Then IndexedExpander.IsExpanded = True End If End If Next End Sub ' Funzione che permette di trovare il primo contenitore di tipo T di un elemento grafico dependencyObject Public Function FindAncestor(Of T As Class)(dependencyObject As DependencyObject) As T Dim target As DependencyObject = dependencyObject Do target = LogicalTreeHelper.GetParent(target) Loop While target IsNot Nothing AndAlso Not (TypeOf target Is T) If IsNothing(target) Then target = dependencyObject Do target = VisualTreeHelper.GetParent(target) Loop While target IsNot Nothing AndAlso Not (TypeOf target Is T) End If Return TryCast(target, T) End Function End Class