如何访问WPF框架导航栏中的控件?

时间:2014-11-12 23:44:46

标签: c# wpf frame

我在Frame的导航栏中添加了StackPanel。 我右键单击我的框架,然后编辑模板和编辑当前,然后我添加了我的命名StackPanel和导航按钮旁边的一些按钮。大!它奏效了。

<Frame  x:Name="MainContent" Content="{Binding CurrentContent, UpdateSourceTrigger=PropertyChanged}" ScrollViewer.VerticalScrollBarVisibility="Visible" 
                    ScrollViewer.CanContentScroll="True" NavigationUIVisibility="Visible" Style="{DynamicResource FrameStyle1}">
                    <Frame.Resources>
                        <!-- Something -->
 <ControlTemplate x:Key="FrameNavChromeTemplateKey" TargetType="{x:Type Frame}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}">
                                <DockPanel>
                                    <Grid Background="{StaticResource NavigationWindowNavigationChromeBackground}" DockPanel.Dock="Top" Height="22">
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="Auto"/>
                                            <ColumnDefinition Width="16"/>
                                            <ColumnDefinition Width="*"/>
                                        </Grid.ColumnDefinitions>
                                        <Menu x:Name="NavMenu" Grid.ColumnSpan="3" Height="16" Margin="1,0,0,0" Style="{StaticResource NavigationWindowMenu}" VerticalAlignment="Center">
                                            <MenuItem ItemContainerStyle="{StaticResource NavigationWindowNavigationButtonJournalEntryStyle}" Padding="0,2,4,0" Style="{StaticResource NavigationWindowMenuItem}">
                                                <MenuItem.ItemsSource>
                                                    <MultiBinding Converter="{StaticResource JournalEntryUnifiedViewConverter}">
                                                        <Binding Path="BackStack" RelativeSource="{RelativeSource TemplatedParent}"/>
                                                        <Binding Path="ForwardStack" RelativeSource="{RelativeSource TemplatedParent}"/>
                                                    </MultiBinding>
                                                </MenuItem.ItemsSource>
                                            </MenuItem>
                                        </Menu>
                                        <Path Grid.ColumnSpan="3" Grid.Column="0" Data="M22.5767,21.035 Q27,19.37 31.424,21.035 A12.5,12.5,0,0,0,53.5,13 A12.5,12.5,0,0,0,37.765,0.926 Q27,4.93 16.235,0.926 A12.5,12.5,0,0,0,0.5,13 A12.5,12.5,0,0,0,22.5767,21.035 z" HorizontalAlignment="Left" IsHitTestVisible="false" Margin="2,0,0,0" SnapsToDevicePixels="false" StrokeThickness="1" VerticalAlignment="Center">
                                            <Path.Fill>
                                                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                    <LinearGradientBrush.GradientStops>
                                                        <GradientStopCollection>
                                                            <GradientStop Color="#A0C2CCE7" Offset="0"/>
                                                            <GradientStop Color="#60FFFFFF" Offset="1"/>
                                                        </GradientStopCollection>
                                                    </LinearGradientBrush.GradientStops>
                                                </LinearGradientBrush>
                                            </Path.Fill>
                                            <Path.LayoutTransform>
                                                <ScaleTransform ScaleY="0.667" ScaleX="0.667"/>
                                            </Path.LayoutTransform>
                                            <Path.Stroke>
                                                <LinearGradientBrush EndPoint="0,1" StartPoint="0,0">
                                                    <LinearGradientBrush.GradientStops>
                                                        <GradientStopCollection>
                                                            <GradientStop Color="#FFB7C2E3" Offset="0.2"/>
                                                            <GradientStop Color="#A0FFFFFF" Offset="0.9"/>
                                                        </GradientStopCollection>
                                                    </LinearGradientBrush.GradientStops>
                                                </LinearGradientBrush>
                                            </Path.Stroke>
                                        </Path>
                                        <Button Grid.Column="0" Margin="3,0,1,0" Style="{StaticResource NavigationWindowBackButtonStyle}">
                                            <Button.LayoutTransform>
                                                <ScaleTransform ScaleY="0.667" ScaleX="0.667"/>
                                            </Button.LayoutTransform>
                                        </Button>
                                        <Button Grid.Column="1" Margin="1,0,0,0" Style="{StaticResource NavigationWindowForwardButtonStyle}">
                                            <Button.LayoutTransform>
                                                <ScaleTransform ScaleY="0.667" ScaleX="0.667"/>
                                            </Button.LayoutTransform>
                                        </Button>
                                        <StackPanel Orientation="Horizontal" Name="CUSTOMNAVBAR" Grid.Column="3" Margin="5,0,10,0">
                                            <Button></Button>
                                            <Button></Button>
                                        </StackPanel>
                                    </Grid>
                                    <ContentPresenter x:Name="PART_FrameCP"/>
                                </DockPanel>
                            </Border>
                            <ControlTemplate.Triggers>
                                <MultiTrigger>
                                    <MultiTrigger.Conditions>
                                        <Condition Property="CanGoForward" Value="false"/>
                                        <Condition Property="CanGoBack" Value="false"/>
                                    </MultiTrigger.Conditions>
                                    <Setter Property="IsEnabled" TargetName="NavMenu" Value="false"/>
                                </MultiTrigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                        <!-- Something -->

 </Frame.Resources>
                </Frame>

但我不知道如何访问此面板..我试过:

        var x = (StackPanel)MainContent.Template.FindName("CUSTOMNAVBAR", MainContent);
        var y = (StackPanel)FindName("CUSTOMNAVBAR");
        var k = (StackPanel)MainContent.FindResource("CUSTOMNAVBAR");
        var i = (StackPanel)MainContent.Resources.FindName("CUSTOMNAVBAR");
        var z = (StackPanel)FindResource("CUSTOMNAVBAR");

它一直返回null或抛出异常......请帮助

2 个答案:

答案 0 :(得分:1)

这应该有效:

var x = (StackPanel)MainContent.Template.FindName("CUSTOMNAVBAR", MainContent);

但是,当Window已经 已经加载 时,请确保运行代码,例如将代码放在某个Loaded事件处理程序中。或者您也可以先尝试调用ApplyTemplate()方法:

MainContent.ApplyTemplate();
var x = (StackPanel)MainContent.Template.FindName("CUSTOMNAVBAR", MainContent);

答案 1 :(得分:0)

要访问后面代码中的元素,您需要设置x:Name指令。你似乎现在使用Name。最好使用数据绑定来获取/设置WPF控件的值,如下所示:

<StackPanel Orientation="Vertical">
                        <Label x:Name="PhotoFileName" Content="{Binding Imageinfo}">

(等)