使用二传手而不是代码进行样式设置

本文关键字:样式 设置 代码 二传手 | 更新日期: 2023-09-27 18:33:47

>我在 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;

的问题,有没有办法用样式和二传手来做到这一点,我如何在可视化树中找到 uname 元素。

编辑

代码隐藏

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>

使用二传手而不是代码进行样式设置

恐怕您必须为此使用可视化树助手(就像您现在正在使用的那样),因为您的控件是第三方控件,因此可能被密封以进行模板覆盖。