Imports EgtWPFLib5 Imports EgtWPFLib5.EgtFloating Public Class OperationExpanderView Private EgtFloatingTray As EgtFloatingTray Private OptionPanelView As UserControl Private OptionPanelViewStackPanel As StackPanel Private MachiningTreeContentPresenter As ContentPresenter Private MachiningTreeUserControl As UserControl Private MachiningTreeExpander As Expander Private SimulationContentPresenter As ContentPresenter Private SimulationUserControl As UserControl Private SimulationExpanderStackPanel As StackPanel Private SimulationExpander As Expander Private GenerateButton As Button 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 EgtFloatingTray = FindAncestor(Of EgtFloatingTray)(Me) OptionPanelView = FindAncestor(Of UserControl)(Me) OptionPanelViewStackPanel = DirectCast(OptionPanelView.Content, StackPanel) MachiningTreeContentPresenter = DirectCast(OptionPanelViewStackPanel.Children(1), ContentPresenter) MachiningTreeUserControl = DirectCast(MachiningTreeContentPresenter.Content, UserControl) MachiningTreeExpander = DirectCast(MachiningTreeUserControl.Content, Expander) SimulationContentPresenter = DirectCast(OptionPanelViewStackPanel.Children(2), ContentPresenter) SimulationUserControl = DirectCast(SimulationContentPresenter.Content, UserControl) SimulationExpanderStackPanel = DirectCast(SimulationUserControl.Content, StackPanel) SimulationExpander = DirectCast(SimulationExpanderStackPanel.Children(0), Expander) GenerateButton = DirectCast(SimulationExpanderStackPanel.Children(1), Button) ' Gestisco l'evento SizeChanged della Tray AddHandler EgtFloatingTray.SizeChanged, AddressOf OperationExpanderTray_SizeChanged m_bFirst = False End If CalculateOperationParametersStackPanelMaxHeight() End Sub Private Sub OperationExpanderTray_SizeChanged(sender As Object, e As System.Windows.SizeChangedEventArgs) CalculateOperationParametersStackPanelMaxHeight() For Each Expander In OperationParametersStackPanel.Children If TypeOf Expander Is Expander Then Dim CurrExpander As Expander = DirectCast(Expander, Expander) CurrExpander.IsExpanded = False End If Next End Sub Private Sub CalculateOperationParametersStackPanelMaxHeight() Dim OccupiedHeight As Double = 0 If Not MachiningTreeExpander.IsExpanded Then OccupiedHeight += MachiningTreeExpander.ActualHeight Else Dim MachiningTreeExpanderContent As FrameworkElement = DirectCast(MachiningTreeExpander.Content, FrameworkElement) MachiningTreeExpanderContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity)) OccupiedHeight += (MachiningTreeExpander.ActualHeight - MachiningTreeExpanderContent.DesiredSize.Height) End If If Not SimulationExpander.IsExpanded Then OccupiedHeight += SimulationExpander.ActualHeight Else Dim SimulationExpanderContent As FrameworkElement = DirectCast(SimulationExpander.Content, FrameworkElement) SimulationExpanderContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity)) OccupiedHeight += (SimulationExpander.ActualHeight - SimulationExpanderContent.DesiredSize.Height) End If OccupiedHeight += GenerateButton.ActualHeight 'Aggiungo altezza OperationsList header Expander If Not OperationsListExpander.IsExpanded Then OccupiedHeight += OperationsListExpander.ActualHeight Else Dim OperationsListExpanderContent As FrameworkElement = DirectCast(OperationsListExpander.Content, FrameworkElement) OperationsListExpanderContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity)) OccupiedHeight += (OperationsListExpander.ActualHeight - OperationsListExpanderContent.DesiredSize.Height) End If If Not OperationParametersExpander.IsExpanded Then OccupiedHeight += OperationParametersExpander.ActualHeight Else Dim OperationParametersExpanderContent As FrameworkElement = DirectCast(OperationParametersExpander.Content, FrameworkElement) OperationParametersExpanderContent.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity)) OccupiedHeight += (OperationParametersExpander.ActualHeight - OperationParametersExpanderContent.DesiredSize.Height) End If ' Aggiungo altezza contenuto OperationParameters che però è fuori dallo Stackpanel OperationFirstParametersStackPanel.Measure(New Size(Double.PositiveInfinity, Double.PositiveInfinity)) If OperationFirstParametersStackPanel.DesiredSize.Height <> 0 Then OccupiedHeight += OperationFirstParametersStackPanel.DesiredSize.Height Else ' aggiungo il valore calcolato provando se non viene calcolato dal sistema al momento OccupiedHeight += 10 End If OperationParametersStackPanel.MaxHeight = OptionPanelViewStackPanel.MaxHeight - OccupiedHeight 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