我在 WPF 应用程序中有一个控件,我需要折叠一个元素,该元素没有暴露,所以我需要像这样在 Loaded 事件上使用代码折叠它。
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;
我的问题是,有没有办法使用样式和设置器来做到这一点,以及如何在可视树中找到未命名的元素。
编辑
后面的代码
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;
}
xml
<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>