已答覆 New to WPF - need some BUTTON help.

  • 2012年4月11日 下午 05:27
     
     
    I want a button that when I click it, it goes to another page in the application.  Is there a simple code for this? 

所有回覆

  • 2012年4月11日 下午 05:32
     
     

    Hello Buddy.

               Its not difficult create object of your form and show it on button click.As below

    var newWindow = new MyNewWindow();
    newWindow.Show();


    Want to add MVP with my name.

  • 2012年4月11日 下午 05:50
     
      包含代碼

     Ok, that didn't work, not sure, but pretty sure, it is me... so here is my code, when I say new to this, I mean NEW to this.  I get the concepts, however, sometimes my brain just doesn't want to put the concepts together and make sense of it. 

    <Button x:Name="btnReturnToStation1" 
                      Content="  Return To Station 1   "
                      DockPanel.Dock="Left"
                      Margin="5"
                      FontSize="28"
                      FontStretch="Expanded"
                      Click="btnReturnToStation1_Click"/>

            private void btnReturnToStation1_Click(object sender, RoutedEventArgs e)
            {
              var uclStation1Measurements = new uclStation1Measurements();
              uclStation1Measurements.Show();
            }

    Can you please tell me what I am doing wrong?  Thanks.
  • 2012年4月11日 下午 05:51
     
     

    This is the error I get with the above code.

    Error 1 'CADC.View.uclStation1Measurements' does not contain a definition for 'Show' and no extension method 'Show' accepting a first argument of type 'CADC.View.uclStation1Measurements' could be found (are you missing a using directive or an assembly reference?) C:\Users\lynosl\Documents\Visual Studio 2010\Projects\CADC\CADC\View\uclStation2Measurements.xaml.cs 37 35 CADC

  • 2012年4月11日 下午 10:49
     
      包含代碼

    Hello lynnosler.

    Did you by chance create a page and not a window?

    If it is a page you could have a frame to show it in and then set it as the source for your frame.

    If it is a window then the above code should work.

    <Window
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	x:Class="WpfApplication13.MainWindow"
    	x:Name="Window"
    	Title="MainWindow"
    	Width="640" Height="480">
    
    	<Grid x:Name="LayoutRoot">
    		<Frame x:Name="myFrame" Content="Frame" Margin="280,0,0,0" NavigationUIVisibility="Hidden"/>
    		<Button Content="Show Page In Frame" HorizontalAlignment="Left" VerticalAlignment="Top" Width="138" Margin="8,8,0,0" Click="getPage"/>
    		<Button Content="Open New Window" HorizontalAlignment="Left" VerticalAlignment="Top" Width="138" Margin="8,63.96,0,0" Click="openWindow"/>
    	</Grid>
    </Window>

    And the Code-Behind...

    using System;
    using System.Windows;
    
    namespace WpfApplication13
    {
    	public partial class MainWindow : Window
    	{
    		public MainWindow()
    		{
    			this.InitializeComponent();
    		}
    
    		private void getPage(object sender, System.Windows.RoutedEventArgs e)
    		{
    			string a = "/WpfApplication13;component/Page1.xaml";
    			myFrame.Source = new Uri(a, UriKind.RelativeOrAbsolute);
    		}
    
    		private void openWindow(object sender, System.Windows.RoutedEventArgs e)
    		{
    			Window2 win2 = new Window2();
    			win2.Show();
    		}
    	}
    }

    Look at the XAML for your "uclStation2Measurements.xaml" and see if the first line starts with "<Page" or is it "<Window".

    Best of luck with your project!

    ~Christine

  • 2012年4月12日 上午 12:10
     
     提議的解答

    Hello dear,

             It seems you used the page navigation method for developing your application. Instead of Button element you should use the Hyperlink element, because the chosen navigation style is for the web model. So you can easily use:

    <Hyperlink NavigateUri="AnotherPageFileName.xaml">
      Another page title
    </Hyperlink>

    Simply you can use the great MSDN to find out how you can use the Hyperlink element. It should come nested with TextBlock element or any other text elements that support document contents. Do not forget to code in nested style. Xaml looks very nice when you set the options to preserve only the first attribute in the begin element's line.

    "Good luck"


    Antonom

  • 2012年4月12日 下午 12:14
     
     

    It is actually a <UserControl> the main window however, is a <Window>

  • 2012年4月12日 下午 04:13
     
     已答覆 包含代碼

    It is actually a <UserControl> the main window however, is a <Window>

    If it is a UserControl it is not really a page like you would assume with a Silverlight project.  It is a control, much like a button or listbox, etc...

    The following is a few ways you could display your UserControl and have it appear to be a "page" or just a control...

    using System;
    using System.Windows;
    
    namespace WpfApplication13
    {
    	public partial class MainWindow : Window
    	{
    		public MainWindow()
    		{
    			this.InitializeComponent();
    		}
    
    		private void openUserControl(object sender, System.Windows.RoutedEventArgs e)
    		{
    			//Adds to the bottom-left of the main window's container (LayoutRoot)
    			myUserControl myUC = new myUserControl();
    			myUC.VerticalAlignment = VerticalAlignment.Bottom;
    			myUC.HorizontalAlignment = HorizontalAlignment.Left;
    			myUC.Width = 200;
    			myUC.Height = 200;
    			this.LayoutRoot.Children.Add(myUC);
    		}
    
    		private void openUserControl2(object sender, System.Windows.RoutedEventArgs e)
    		{
    			//Adds to the main window's container (LayoutRoot) and fills
    			//the entire window.
    			myUserControl myUC = new myUserControl();
    			myUC.VerticalAlignment = VerticalAlignment.Stretch;
    			myUC.HorizontalAlignment = HorizontalAlignment.Stretch;
    			this.LayoutRoot.Children.Add(myUC);
    		}
    
    		private void openUserControl3(object sender, System.Windows.RoutedEventArgs e)
    		{
    			//Opens a new instance of the MainWindow, Clears the default contents
    			//and places the UserControl in it.
    			MainWindow win2 = new MainWindow();
    			win2.LayoutRoot.Children.Clear();
    			myUserControl myUC = new myUserControl();
    			myUC.VerticalAlignment = VerticalAlignment.Stretch;
    			myUC.HorizontalAlignment = HorizontalAlignment.Stretch;
    			win2.LayoutRoot.Children.Add(myUC);
    			win2.Show();
    		}
    
    		private void openUserControl4(object sender, System.Windows.RoutedEventArgs e)
    		{
    			//Opens the UserControl in myFrame on the MainWindow
    			this.myFrame.Source = new Uri("../WpfApplication13;component/myUserControl.xaml", UriKind.RelativeOrAbsolute);
    		}
    	}
    }

    And my xaml...

    <Window
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	xmlns:local="clr-namespace:WpfApplication13" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"
    	x:Class="WpfApplication13.MainWindow"
    	x:Name="Window"
    	Title="MainWindow"
    	Width="640" Height="480">
    
    	<Grid x:Name="LayoutRoot">
    		<Button Content="Open UserControl in Same Window" HorizontalAlignment="Left" VerticalAlignment="Top" Width="288" Margin="8,8,0,0" Click="openUserControl"/>
    		<Button Content="Open UserControl in Same Window (Enitre Window)" HorizontalAlignment="Left" VerticalAlignment="Top" Width="288" Margin="8,33.96,0,0" Click="openUserControl2"/>
    		<Button Content="Open UserControl in a new instance of MainWindow" HorizontalAlignment="Left" VerticalAlignment="Top" Width="288" Margin="8,59.92,0,0" Click="openUserControl3"/>
    		<Button Content="Open UserControl in a Frame on the MainWindow" HorizontalAlignment="Left" VerticalAlignment="Top" Width="288" Margin="8,85.88,0,0" Click="openUserControl4"/>
    		<Frame x:Name="myFrame" Content="Frame" Margin="300,8,8,8" NavigationUIVisibility="Hidden"/>
    	</Grid>
    </Window>

    However if you truly want it to behave as it's own window, it would probably best to turn it into a window.  The above wouldn't be considered best practice in most cases.  But ultimately, only you can decide what is best for your project.

    :) Best of luck.

    ~Christine

    ...

    To clarify a bit, I named the userControl I used in the example "myUserControl".  You would replace that with the name of your userControl.

    ...

    I got to thinking about this... Are you by chance creating a Silverlight project for the web and not a Desktop or WPF program? 



    • 已編輯 Christine L. _ 2012年4月12日 下午 04:16
    • 已編輯 Christine L. _ 2012年4月12日 下午 04:26
    • 已編輯 Christine L. _ 2012年4月12日 下午 04:38
    • 已標示為解答 lynnosler 2012年4月16日 下午 02:23
    • 已取消標示為解答 lynnosler 2012年4月16日 下午 02:23
    • 已標示為解答 lynnosler 2012年4月16日 下午 02:29
    •  
  • 2012年4月16日 下午 02:29
     
     

    I am building a kiosk like system with 2 stations and a control panel on a seperate PC.  This is my first big project and my boss has done the layout work, so I have to go with what he has.   Your code Christine helped!  I am still working on alot of learning.  I have the xaml pretty much down, it's the code behind I am struggling with.

    I need one of the buttons to release the focus from one screen and put it onto the other screen, is there a way to do that?