none
Opening a new window in WPF

    Question

  • Hi,

    I am new to WPF and was trying to open a window on click of a button by using ShowDialog method.

    It opens a new window but displays it as another window in task bar. How can we get this window as we get while displaying alerts on click of button.

    • Moved by Bob Wu-MT Monday, August 27, 2012 9:27 AM (From:Windows Forms Designer)
    Sunday, August 26, 2012 2:49 PM

Answers

  •         
    Hi John,

    set "ShowInTaskbar" property to false for your newly created window like

    ShowInTaskbar="False"

    <Window x:Class="WpfApplication23.ChildWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            ShowInTaskbar="False"
        Title="ChildWindow" Height="300" Width="300">
        <Grid>

        </Grid>
    </Window>

    also set the Owner property like

    ChildWindow cWindow = new ChildWindow();
    cWindow.Owner = this;
    cWindow.Show();

    Please let me know if you have any query.

    Regards

    Ashish

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

    Please remember to mark the replies as answered if they help.
    Monday, August 27, 2012 10:23 AM
  • If you need to place your window into the notification area rather than displaying it on the screen, you can do this:

    Set the WindowState property on the window to Minimized and its ShowInTaskbar property to false. Now the window will not be show on the screen or in the taskbar, when you call the ShowDialog.

    To place the window to the notification area, use the NotifyIcon class in the System.Windows.Forms namespace. It will palce an icon to the notification area. You can hook to its Click or DoubleClick (or any suitable) event and restore your window.

    The following example represents a window, that is shown in the notification area. When the user double clicks on the icon in the notification area, the window is restored (shown). When the user tries to close the window, it is minimized - "placed back to the notification area". Note, that I`m setting the Title of the window to an empty string. When you to this, your window will not be shown in the task manager.

    <Window x:Class="WpfApplication23.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            WindowState="Minimized" StateChanged="Window_StateChanged" Closing="Window_Closing">
    </Window>
    
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                NotifyIcon notifyIcon = new NotifyIcon();
                notifyIcon.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("WpfApplication23.Icon1.ico"));
                notifyIcon.Visible = true;
                notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
    
                Update();
            }
    
            void notifyIcon_DoubleClick(object sender, EventArgs e)
            {
                WindowState = System.Windows.WindowState.Normal;
            }
    
            private void Update()
            {
                if (WindowState == System.Windows.WindowState.Minimized)
                {
                    ShowInTaskbar = false;
                    Title = string.Empty;
                }
                else
                {
                    ShowInTaskbar = true;
                    Title = "Test";
                }
            }
    
            private void Window_StateChanged(object sender, EventArgs e)
            {
                Update();
            }
    
            private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                WindowState = System.Windows.WindowState.Minimized;
                e.Cancel = true;
            }        
        }

    • Marked as answer by Annabella LuoModerator Monday, September 03, 2012 10:01 AM
    • Unmarked as answer by john0609 Monday, September 10, 2012 11:57 AM
    • Marked as answer by john0609 Monday, September 10, 2012 11:57 AM
    Monday, August 27, 2012 10:28 AM

All replies

  • Hi John,

    According to your description, this is a wpf issue. I will move it to wpf forum for better support.

    Best Regards,


    Bob Wu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, August 27, 2012 9:27 AM
  •         
    Hi John,

    set "ShowInTaskbar" property to false for your newly created window like

    ShowInTaskbar="False"

    <Window x:Class="WpfApplication23.ChildWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            ShowInTaskbar="False"
        Title="ChildWindow" Height="300" Width="300">
        <Grid>

        </Grid>
    </Window>

    also set the Owner property like

    ChildWindow cWindow = new ChildWindow();
    cWindow.Owner = this;
    cWindow.Show();

    Please let me know if you have any query.

    Regards

    Ashish

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

    Please remember to mark the replies as answered if they help.
    Monday, August 27, 2012 10:23 AM
  • If you need to place your window into the notification area rather than displaying it on the screen, you can do this:

    Set the WindowState property on the window to Minimized and its ShowInTaskbar property to false. Now the window will not be show on the screen or in the taskbar, when you call the ShowDialog.

    To place the window to the notification area, use the NotifyIcon class in the System.Windows.Forms namespace. It will palce an icon to the notification area. You can hook to its Click or DoubleClick (or any suitable) event and restore your window.

    The following example represents a window, that is shown in the notification area. When the user double clicks on the icon in the notification area, the window is restored (shown). When the user tries to close the window, it is minimized - "placed back to the notification area". Note, that I`m setting the Title of the window to an empty string. When you to this, your window will not be shown in the task manager.

    <Window x:Class="WpfApplication23.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            WindowState="Minimized" StateChanged="Window_StateChanged" Closing="Window_Closing">
    </Window>
    
    public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                NotifyIcon notifyIcon = new NotifyIcon();
                notifyIcon.Icon = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream("WpfApplication23.Icon1.ico"));
                notifyIcon.Visible = true;
                notifyIcon.DoubleClick += new EventHandler(notifyIcon_DoubleClick);
    
                Update();
            }
    
            void notifyIcon_DoubleClick(object sender, EventArgs e)
            {
                WindowState = System.Windows.WindowState.Normal;
            }
    
            private void Update()
            {
                if (WindowState == System.Windows.WindowState.Minimized)
                {
                    ShowInTaskbar = false;
                    Title = string.Empty;
                }
                else
                {
                    ShowInTaskbar = true;
                    Title = "Test";
                }
            }
    
            private void Window_StateChanged(object sender, EventArgs e)
            {
                Update();
            }
    
            private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                WindowState = System.Windows.WindowState.Minimized;
                e.Cancel = true;
            }        
        }

    • Marked as answer by Annabella LuoModerator Monday, September 03, 2012 10:01 AM
    • Unmarked as answer by john0609 Monday, September 10, 2012 11:57 AM
    • Marked as answer by john0609 Monday, September 10, 2012 11:57 AM
    Monday, August 27, 2012 10:28 AM
  • thanks ashish.. it worked
    Tuesday, September 11, 2012 4:12 AM
  • thanks Ivan, it is very helpful
    Tuesday, September 11, 2012 4:13 AM