从C#中查找Xaml中的x:名称

时间:2014-01-28 09:08:12

标签: c# wpf xaml

从获得一点帮助后,我设法让窗口只打开一次,现在我想将窗口更改为页面。当我这样做时,显然.Show();.Close();没有扩展方法。

现在我在我的Generic页面添加了一个框架(因为这将在所有表格中):

<Frame x:Name="FrameNavigate" HorizontalAlignment="Left" Height="300" Margin="1296,52,0,0" VerticalAlignment="Top" Width="300" NavigationUIVisibility="Hidden"/>

我把x:Name"FrameNavigate"放在XAML中。在Generic页面后面的代码中,我想添加这段代码以在框架上打开页面。

private void btnHelp_Click(object sender, RoutedEventArgs e)
{
    if (help != null)
    {
        help.Close();
        help = null;
    }
    else
    {
        help = new xamlHelp();
        FrameNavigate.Navigate(new xamlHelp());
    }
}

但它说FrameNavigate不存在?

编辑:

<Style TargetType="{x:Type local:Master}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:Master}">
                <StackPanel>
                    <Canvas Height="50" Margin="0,0,0,0" HorizontalAlignment="Stretch" FlowDirection="RightToLeft">
                        <Canvas.Background>
                            <LinearGradientBrush EndPoint="0,0" StartPoint="0,1">
                                <GradientStop Color="#FFB3DDF2" Offset="1.0"/>
                                <GradientStop Color="#FFD6E9F4" Offset="0.0"/>
                            </LinearGradientBrush>
                        </Canvas.Background>
                        <Button x:Name="btnHelp" Content="Help" Click="btnHelp_Click" Foreground="#FF7E8385" FontFamily="Calibri" FontSize="18" Margin="110,10,0,0" Height="30" Width="70" Style="{x:Null}" BorderBrush="Transparent" Background="Transparent" Cursor="Hand"/>
                        <GridSplitter Height="30" Width="1" Margin="95,10,0,0" Background="Gray"/>
                        <Button x:Name="btnSettings" Content="Settings" Foreground="#FF7E8385" FontFamily="Calibri" FontSize="18" Margin="10,10,0,0" Click="btnSettings_Click" Height="30" Width="70" Style="{x:Null}" BorderBrush="Transparent" Background="Transparent" Cursor="Hand"/>
                    </Canvas>
                    <Canvas Width="350" Height="850" Margin="0,0,0,0" VerticalAlignment="Stretch" HorizontalAlignment="Left" FlowDirection="RightToLeft" DockPanel.Dock="Bottom">
                        <Canvas.Background>
                            <LinearGradientBrush EndPoint="0,0" StartPoint="0,1">
                                <GradientStop Color="#FFD6E9F4" Offset="1.0"/>
                                <GradientStop Color="White" Offset="0.0"/>
                            </LinearGradientBrush>
                        </Canvas.Background>
                        <Frame x:Name="FrameNavigate" HorizontalAlignment="Left" Height="300" Margin="1296,52,0,0" VerticalAlignment="Top" Width="300" NavigationUIVisibility="Hidden"/>
                    </Canvas>
                </StackPanel>
            </ControlTemplate> 
        </Setter.Value>
    </Setter>
</Style>

编辑:

我有这个没有错误的代码,但是当你点击按钮时没有出现任何错误?

Master master = ((Button)sender).TemplatedParent as Master;
Frame frame = (Frame)master.Template.FindName("FrameNavigate",master);
frame.Navigate(new xamlHelp());

2 个答案:

答案 0 :(得分:2)

由于Frame中存在ControlTemplate,因此您无法直接从代码中直接访问它。

要求模板使用FrameworkTemplate.FindName方法为您提供。

此外,您需要获得Master控制权(显然是获取主控制的模板),您可以通过accessing TemplatedParent of sender button获取。

您需要这样做:

Master master = ((Button)sender).TemplatedParent as Master;
Frame frame = (Frame)master.Template.FindName("FrameNavigate", master);
frame.Navigate(new xamlHelp());

答案 1 :(得分:1)

更新:

Navigate(TypeName)方法采用Type对象!

Frame frame = (Frame)this.FindName("FrameNavigate");
frame.Navigate(typeof(xamlHelp)); //frame.Navigate(help);

看看: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.navigate.aspx

并在: http://msdn.microsoft.com/en-us/library/windows/apps/hh771188.aspx

PS。请注意,您没有在其他代码块中使用help变量。