我在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;
我的问题是,有没有办法使用样式和设置器来完成此操作,如何在可视化树中找到 unamed 元素。
修改
背后的代码
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>
答案 0 :(得分:0)
我担心你必须使用视觉树帮助器(就像你现在正在使用的那样),因为你的控件是第三方控件,因此可能会因为模板覆盖而被密封。