Ben
Ben

Reputation: 247

Finding the x:Name in Xaml from C#

From getting a little help, I managed to make the window only open once, now I want to change the window to a page. When I do this obviously .Show(); and also .Close(); have no extension method.

Now I added a frame to my Generic page (As this will be on all forms):

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

And I put the x:Name"FrameNavigate" in the XAML. In the code behind the Generic page, I wanted to add this piece of code to open the page up on the frame.

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

But It says that FrameNavigate doesn't exist?

EDIT:

<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>

EDIT:

I have this code which has got no errors, but when you click the button nothing appears?

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

Upvotes: 0

Views: 2950

Answers (2)

Rohit Vats
Rohit Vats

Reputation: 81313

Since Frame exists in ControlTemplate, you can't access it directly like this from code behind.

Ask template to get that for you by using FrameworkTemplate.FindName method.

Also you need to get Master control (obviously to get template of Master control) which you can get via accessing TemplatedParent of sender button.

This is how you need to do it:

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

Upvotes: 2

Fredrik
Fredrik

Reputation: 2317

UPDATED:

The Navigate(TypeName) method takes a Type object!

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

Have a look at: http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.controls.frame.navigate.aspx

and at: http://msdn.microsoft.com/en-us/library/windows/apps/hh771188.aspx

ps. Note you are not using your help variable in you else code block.

Upvotes: 1

Related Questions