locked
how to navigate from one WPF form to another????? RRS feed

  • Question

  • hi,

    i created a project in c# and added the wpf windows in the expression blend 4. After adding these windows what all that i can see in the solution explorer are the Window_name.xaml files. now i have created a button in the MainWindow.xaml window, now i want to navigate form this WPF window to the another WPF window named as Window2.xaml on the button click.

    can any one suggest me the possible code in c# ??????

     

     

    • Moved by Rudedog2 Thursday, August 26, 2010 1:48 PM : Issue is Off-Topic (From:Visual C# General)
    Wednesday, August 25, 2010 3:35 AM

Answers

All replies

  • You can add an hyperlink using the "NavigateUri" attribute... something like:

    <Hyperlink NavigateUri="Window2.xaml">
      Navigate to Another Page
    </Hyperlink>

    Here is the documentation you need : http://msdn.microsoft.com/en-us/library/ms750478.aspx.

    Wednesday, August 25, 2010 5:59 AM
  • hi,

    Thnx for your reply.

    -> but the coode that you have suggested is in XAML. It will be great if you can suggest me a equivelent c# code

    -> also can you further elaborate that how can i associate this hyperlink navigation with a button click event??????

    Wednesday, August 25, 2010 6:30 AM
  • -> a "C# code" would probably something resembling to:

    Application.Current.MainWindow = newWindow; 
                    newWindow
    .Show(); 
                    sourceWindow
    .Close(); 

    On the other hand, I don't think it's a good idea.

    -> Did you read the doc I sent ? The documentation is a complete step-by-step example, I won't copy/paste the article. Instead, it would be better if you tell us what is not clear or what is not working for you with this example.

    Wednesday, August 25, 2010 10:19 AM
  • hi,

     

    i've got you a bit more clearer this time, but i am not creating this application as the web browser based application rather i have created this application as the windows form based application and i want this application to navigate in the way the windows form based applications does without using the web browser.

    Here i am giving you the code i've used to create the navigation in one of my windows form application.

    can you please suggest the possible modifications in this code which i can use in this WPF project of mine.

     

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;

    namespace App
    {
    public partial class Form1 : Form
    {

    public Form1()
    {
    InitializeComponent();
    }

    public static void ThreadProc1()
    {
    Application.Run(new Form2());
    }
    private void button1_Click(object sender, EventArgs e)
    {

    System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ThreadProc));
    t.Start();
    this.Close();
    }

    }

    Wednesday, August 25, 2010 10:49 AM
    • Proposed as answer by Rudedog2 Thursday, August 26, 2010 1:47 PM
    • Marked as answer by Rudedog2 Thursday, August 26, 2010 1:47 PM
    Wednesday, August 25, 2010 2:39 PM
  • ^bump^
    Mark the best replies as answers. "Fooling computers since 1971."

    http://rudedog2.spaces.live.com/default.aspx

    Thursday, August 26, 2010 1:47 PM
  • hi,

     

    I've tried the following code:

     

    using System;
    using System.Collections;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;
    using System.Windows.Navigation;
    namespace MG
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public ScanHome sh = new ScanHome();
            public MainWindow()
            {
                this.InitializeComponent();
                
                // Insert code required on object creation below this point.

            }

          
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                this.NavigationService.Navigate(new Uri("ScanHome.xaml ", UriKind.Relative));

            }
           
          
          
        }
    }

     

     

    where the "ScanHome" is yet another window in my project.

    But when i built this project it gave the following error:

     

    error CS1061: 'MG.MainWindow' does not contain a definition for 'NavigationService' and no extension method 'NavigationService' accepting a first argument of type 'MG.MainWindow' could be found (are you missing a using directive or an assembly reference?)

     

     

    can any one help me out resolving this error???

    Saturday, August 28, 2010 11:41 AM
  • Tuesday, August 31, 2010 7:39 PM
  • For Navigated to window,  you should create object from the window that you want to navigate to it, and use ShowDialog() method,

    Ex:

    I need to navigate to window that's called Window2.xaml

    Window2 win2 = new Window2();
    win2.ShowDialog();

    -----------------------------------------

    For navigate to page, I prefer to use Frame control

    <Frame x:Name="navigatedFram" Margin="10"/>

    In the backend assign the page that you want to navigate to it.

    NavigatedPage nPage = new NavigatedPage();
    navigatedFram.Navigate(nPage);

    I hope this is helping you :)

    Thanks


    Monday, January 27, 2014 11:19 AM
  • Hi, I don't know if this still help.

    I am working WPF for window desktop programming and have problem with switching between from.

    I use navigationservice as i used in Window Phone Programming. However i got stuck.

    But when i try to use the code i use in window form

    Another_window_name main = new Another_window_name();       

    this.Hide();            main.Show();

    And this is work. 

    Monday, May 26, 2014 6:02 AM