none
Open window in C# wpf

    Question

  • Hello, i have two windows in my project, and have a button in the main window and when i press it i want window2 to open. How do this work in C#
    Sunday, July 26, 2009 9:18 PM

Answers

All replies

  • Although I have not used WPF often, I think this should be as simple as in Winform. Tried this and it worked as i expected: 

    VB:          Dim w2 As Window2 = New Window2()
                   w2.Show()
    C# should be 
                 Window2 w2 = New Window2();
                 w2.Show();

    • Marked as answer by Alcstudio Sunday, July 26, 2009 10:58 PM
    • Unmarked as answer by Alcstudio Sunday, July 26, 2009 11:03 PM
    Sunday, July 26, 2009 10:10 PM
  • Thanks for the repsonse.

    It works but it opens a blank window, my window have a interface. 
    Sunday, July 26, 2009 10:58 PM
  • Thanks for the repsonse.

    It works but it opens a blank window, my window have a interface. 

    Can you show the actual code you tried? Also, does the designer show your window's child controls correctly?
    http://blog.voidnish.com
    Monday, July 27, 2009 12:14 AM
  • The Code i tried was this:

    Window1 w2 = new Window1();
    w2.Show(); 
    Monday, July 27, 2009 12:22 AM
  • The Code i tried was this:

    Window1 w2 = new Window1();
    w2.Show(); 

    Window1 is your main window, right? The 2nd window that you want to call is probably Window2 (just a guess). If so you need to do :

    Window2 w2 = new Window2();
    w2.Show();
    http://blog.voidnish.com
    Monday, July 27, 2009 12:24 AM
  • My main window is LoginWindow =), and my second window is window1 hehe =), should be the other way around but =)
    Monday, July 27, 2009 12:28 AM
  • My main window is LoginWindow =), and my second window is window1 hehe =), should be the other way around but =)

    Ok, so this Window1 (your 2nd window), does it show up correctly in the designer?
    http://blog.voidnish.com
    Monday, July 27, 2009 12:29 AM
  • Yes its show up correctly in the designer.
    Monday, July 27, 2009 12:33 AM
  • Yes its show up correctly in the designer.

    That's weird (typically you have the reverse problem). Can you paste the xaml here?
    http://blog.voidnish.com
    Monday, July 27, 2009 12:35 AM
  • Here is the xaml code for window1:

    <Window
    	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    	x:Class="Window1"
    	x:Name="Window"
    	Title="Window1"
    	Width="608" Height="448" Background="{x:Null}" AllowsTransparency="True" ResizeMode="CanResizeWithGrip" WindowStyle="None" xmlns:Secure_Archiving="namespace:Secure_Archiving" WindowStartupLocation="CenterScreen" Icon="Resources/taskbarlogo.png">
        <Window.Resources>
        </Window.Resources>
        <Window.Triggers>
        </Window.Triggers>
        <Grid x:Name="LayoutRoot">
            <Rectangle HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Stroke="{x:Null}" RadiusX="8" RadiusY="8">
                <Rectangle.Fill>
                    <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                        <GradientStop Color="#FF8BC400" Offset="0"/>
                        <GradientStop Color="#FF4A6800" Offset="0.991"/>
                    </LinearGradientBrush>
                </Rectangle.Fill>
            </Rectangle>
            <Label Foreground="#FF626262" Opacity="1" Background="#00000000" FontFamily="Gautami" FontSize="13" FontWeight="Normal" HorizontalAlignment="Center" Margin="0,-1,0,0" VerticalAlignment="Top" Height="30" Content="Secure Archiving"/>
            <Menu Background="{x:Null}" Foreground="#FFFFFFFF" HorizontalAlignment="Left" Margin="5,5,0,0" VerticalAlignment="Top" Width="198" Height="27">
                <MenuItem Header="File"/>
                <MenuItem Header="Edit"/>
                <MenuItem Header="Download"/>
                <MenuItem Header="Settings"/>
            </Menu>
            <Canvas HorizontalAlignment="Left" Margin="8,28,0,0" x:Name="SearchBox" VerticalAlignment="Top" Width="256" Height="25">
                <Rectangle Fill="#FFFFFFFF" Visibility="Visible" Width="256" Height="25" Stroke="{x:Null}" RadiusX="5" RadiusY="5"/>
                <TextBox Foreground="#FF000000" BorderBrush="{x:Null}" Background="{x:Null}" BorderThickness="0,0,0,0" Width="234" Height="21" Canvas.Left="8" Canvas.Top="2" Text="" TextWrapping="Wrap"/>
            </Canvas>
        </Grid>
    </Window>

    Monday, July 27, 2009 12:39 AM
  • Nothing wrong there as far as I can see. When you run it, you get a blank white window? No green background and no menu?
    http://blog.voidnish.com
    Monday, July 27, 2009 1:17 AM
  • no menu no green. its wpf btw, if it have something to do with that. 

    Its just white.
    Monday, July 27, 2009 1:19 AM
  • no menu no green. its wpf btw, if it have something to do with that. 

    Its just white.

    Okay, I copy/pasted that into a test project and it runs fine. Perhaps you are doing something in code - can you post the cs file too?
    http://blog.voidnish.com
    Monday, July 27, 2009 1:23 AM
  • It is possible that you are not calling InitializeComponent in the window constructor. If so that's your problem.
    http://blog.voidnish.com
    Monday, July 27, 2009 1:26 AM
  • Did not have the InitializeComponent(); in the Public Window1()

    But if i insert it i get error

    The name 'InitializeComponent' does not exist in the current context.

    The CS code looks like this.

    using MySql.Data.MySqlClient;
    using MySql.Data;
    using System;
    using System.Data;
    using System.Configuration;
    using System.ComponentModel;
    using System.Collections.Generic;
    using System.Linq;
    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.Data.Common;
    using System.Diagnostics;
    
    namespace SecureArchiving
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
            }
        }
    }
    

    • Edited by Alcstudio Monday, July 27, 2009 1:44 AM some C# code
    Monday, July 27, 2009 1:43 AM
  • Your project doesn't seem to be correctly setup for xaml compilation. Did you manually create a new C# project and then add references?
    http://blog.voidnish.com
    Monday, July 27, 2009 1:59 AM
  • The Build Action for the xaml file must be set to Page, and Custom Tool set to MSBuild:Compile.
    http://blog.voidnish.com
    Monday, July 27, 2009 2:02 AM
  • The Build Action for window1 is set to Page, and the Custom Tool is set to MSBuild:Compile
    Monday, July 27, 2009 2:06 AM
  • The Build Action for window1 is set to Page, and the Custom Tool is set to MSBuild:Compile


    Change :

    <Window
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class="Window1"

    to :

    <Window
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     x:Class="SecureArchiving.Window1"


    http://blog.voidnish.com
    • Marked as answer by Alcstudio Monday, July 27, 2009 2:16 AM
    Monday, July 27, 2009 2:13 AM
  • Once you make the above change, you should be able to call InitializeComponent in the constructor.
    http://blog.voidnish.com
    Monday, July 27, 2009 2:14 AM
  • Now its works as a charm =)
    Monday, July 27, 2009 2:16 AM
  • window2Obj.show();

    - daya
    Monday, July 27, 2009 6:44 AM