I have a control in WPF application where i need to collapse a element the element is not exposed so i need to collapse it with code on the Loaded event like this.
Stema.Controls.NavigationPane navPane = (Stema.Controls.NavigationPane)sender;
DockPanel docPanel =(DockPanel)VisualTreeHelper.GetChild(navPane, 0);
Border border = (Border)VisualTreeHelper.GetChild(docPanel, 1);
Grid grid = (Grid)VisualTreeHelper.GetChild(border, 0);
DockPanel docPanel1 = (DockPanel)VisualTreeHelper.GetChild(grid, 4);
docPanel1.Visibility = Visibility.Collapsed;
My question, is there a way to do this with a style and a setter and how do i find the unamed element in the visual tree.
EDIT
code behind
private void NavPane_Loaded(object sender, RoutedEventArgs e)
{
Stema.Controls.NavigationPane navPane = (Stema.Controls.NavigationPane)sender;
DockPanel docPanel =(DockPanel)VisualTreeHelper.GetChild(navPane, 0);
Border border = (Border)VisualTreeHelper.GetChild(docPanel, 1);
Grid grid = (Grid)VisualTreeHelper.GetChild(border, 0);
DockPanel docPanel1 = (DockPanel)VisualTreeHelper.GetChild(grid, 4);
docPanel1.Visibility = Visibility.Collapsed;
}
xaml
<s:NavigationPane Loaded="NavPane_Loaded" x:Name="navigationPane" SelectedIndex="0" LargeItems="2" Margin="2,0,0,1" IsMinimized="False" Width="200" DockPanel.Dock="Left" Background="White" >
<s:NavigationPaneItem Header="Reportitems" Name="intro" >
<ContentControl x:Name="ActionContent" prism:RegionManager.RegionName="{x:Static inf:RegionNames.LeftNavigationRegion}" VerticalAlignment="Stretch" >
<ContentControl.Template>
<ControlTemplate TargetType="ContentControl">
<ContentPresenter Content="{TemplateBinding Content}" />
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="false">
<Setter Property="Visibility" Value="Collapsed" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</ContentControl.Template>
</ContentControl>
</s:NavigationPaneItem>
</s:NavigationPane>
I am afraid you have to use the visual tree helper (like you are using right now) for this because your control is a third party one and thus is probably sealed for template overriding.