WPF publish initial content (#1032)
* Add overview article for new WPF (#178) * Remove previous WPF .NET 5 content * Overview article * New article: Create new WPF project (#183) * Basic index file * Finish article for new project. * acro * markdown fix * Fix build errors * fix code lang * fix code lang * Add differences article for WPF (#186) * Fix VS version for create app * Add differences article * Minor * fix overview styles code * Add code langs for overview * Port Window Overview WPF article (#1011) * Add Window overview article * Add TOC * Remove temp code * Fix headers * Port final Windows related articles (#1015) * Initial test commit * Add system dialogs * 75% complete * Add images * Fix linter * Add to toc * Add more howto * Fix code * Add get/set main window * Add missing code * Add to toc * Fix warnings * Fix warnings * missing code * Update see also * fix xref * Migrate wpf net core controls-styles articles (#1023) * Migrate control-styles * Fix links * Fix links * Port databinding article. (#1022) * Migrated databinding overview * update toc * Fix links/snippets * fix links * Move to correct folder * Port initial resources docs (#1026) * initial resources docs * Build errors * Update dotnet-desktop-guide/net/wpf/systems/xaml-resources-overview.md Co-authored-by: Genevieve Warren <[email protected]> * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * Feedback * Add app article * Add system article * Minor * meta * fix toc * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> Co-authored-by: Genevieve Warren <[email protected]> * Add xaml article * reformat redirect * Redirects * WPF update XAML article (#1031) * convert snippets * minor edits * minor * Fix warnings * Fixes #1028 * Fixes #1028 * Apply suggestions from code review Co-authored-by: Genevieve Warren <[email protected]> * update redirects Co-authored-by: Genevieve Warren <[email protected]> * minor updates to link * Readd migration article * Warning fixes * Fix lint * Minor updates * Fix warnings for TOC/YML * last warnings Co-authored-by: Genevieve Warren <[email protected]>
@@ -0,0 +1,171 @@
|
||||
---
|
||||
title: Dialog Boxes Overview
|
||||
description: Learn about the varieties of dialog boxes in Windows Foundation Presentation (WPF). With a dialog box, you gather and display information to a user.
|
||||
ms.date: "03/12/2021"
|
||||
helpviewer_keywords:
|
||||
- "modeless dialog boxes [WPF]"
|
||||
- "modal dialog boxes [WPF]"
|
||||
- "dialog boxes [WPF]"
|
||||
- "message boxes [WPF]"
|
||||
---
|
||||
|
||||
# Dialog boxes overview (WPF .NET)
|
||||
|
||||
Windows Presentation Foundation (WPF) provides ways for you to design your own dialog boxes. Dialog boxes are windows but with a specific intent and user experience. This article discusses how a dialog box works and what types of dialog boxes you can create and use. Dialog boxes are used to:
|
||||
|
||||
- Display specific information to users.
|
||||
- Gather information from users.
|
||||
- Both display and gather information.
|
||||
- Display an operating system prompt, such a print window.
|
||||
- Select a file or folder.
|
||||
|
||||
These types of windows are known as _dialog boxes_. A dialog box can be displayed in two ways: modal and modeless.
|
||||
|
||||
Displaying a _modal_ dialog box to the user is a technique with which the application interrupts what it was doing until the user closes the dialog box. This generally comes in the form of a prompt or alert. Other windows in the application can't be interacted with until the dialog box is closed. Once the _modal_ dialog box is closed, the application continues. The most common dialog boxes are used to show an open file or save file prompt, displaying the printer dialog, or messaging the user with some status.
|
||||
|
||||
A *modeless* dialog box doesn't prevent a user from activating other windows while it's open. For example, if a user wants to find occurrences of a particular word in a document, a main window will often open a dialog box to ask a user what word they're looking for. Since the application doesn't want to prevent the user from editing the document, the dialog box doesn't need to be modal. A modeless dialog box at least provides a **Close** button to close the dialog box. Other buttons may be provided to run specific functions, such as a **Find Next** button to find the next word in a word search.
|
||||
|
||||
With WPF you can create several types of dialog boxes, such as message boxes, common dialog boxes, and custom dialog boxes. This article discusses each, and the [Dialog Box Sample](https://github.com/Microsoft/WPF-Samples/tree/master/Windows/DialogBox) provides matching examples.
|
||||
|
||||
## Message boxes
|
||||
|
||||
A *message box* is a dialog box that can be used to display textual information and to allow users to make decisions with buttons. The following figure shows a message box that asks a question and provides the user with three buttons to answer the question.
|
||||
|
||||
:::image type="content" source="media/dialog-boxes-overview/word-processor-dialog.png" alt-text="Word processor dialog box asking if you want to save the changes to the document before the application closes.":::
|
||||
|
||||
To create a message box, you use the <xref:System.Windows.MessageBox> class. <xref:System.Windows.MessageBox> lets you configure the message box text, title, icon, and buttons.
|
||||
|
||||
For more information, see [How to open a message box](how-to-open-message-box.md).
|
||||
|
||||
## Common dialog boxes
|
||||
|
||||
Windows implements different kinds of reusable dialog boxes that are common to all applications, including dialog boxes for selecting files and printing.
|
||||
|
||||
Since these dialog boxes are provided by the operating system, they're shared among all the applications that run on the operating system. These dialog boxes provide a consistent user experience, and are known as *common dialog boxes*. As a user uses a common dialog box in one application, they don't need to learn how to use that dialog box in other applications.
|
||||
|
||||
WPF encapsulates the open file, save file, and print common dialog boxes and exposes them as managed classes for you to use in standalone applications.
|
||||
|
||||
:::image type="content" source="media/dialog-boxes-overview/open-file-dialog-box.png" alt-text="Open file dialog box called from WPF.":::
|
||||
|
||||
To learn more about common dialog boxes, see the following articles:
|
||||
|
||||
- [How to display a common dialog box](how-to-open-common-system-dialog-box.md)
|
||||
- [Show the Open File dialog box](how-to-open-common-system-dialog-box.md#open-file-dialog-box)
|
||||
- [Show the Save File dialog box](how-to-open-common-system-dialog-box.md#save-file-dialog-box)
|
||||
- [Show the Print dialog box](how-to-open-common-system-dialog-box.md#print-dialog-box)
|
||||
|
||||
## Custom dialog boxes
|
||||
|
||||
While common dialog boxes are useful, and should be used when possible, they don't support the requirements of domain-specific dialog boxes. In these cases, you need to create your own dialog boxes. As we'll see, a dialog box is a window with special behaviors. <xref:System.Windows.Window> implements those behaviors and you use the window to create custom modal and modeless dialog boxes.
|
||||
|
||||
There are many design considerations to take into account when you create your own dialog box. Although both an application window and dialog box contain similarities, such as sharing the same base class, a dialog box is used for a specific purpose. Usually a dialog box is required when you need to prompt a user for some sort of information or response. Typically the application will pause while the dialog box (modal) is displayed, restricting access to the rest of the application. Once the dialog box is closed, the application continues. Confining interactions to the dialog box alone, though, isn't a requirement.
|
||||
|
||||
When a WPF window is closed, it can't be reopened. Custom dialog boxes are WPF windows and the same rule applies. To learn how to close a window, see [How to close a window or dialog box](how-to-close-window-dialog-box.md).
|
||||
|
||||
### Implementing a dialog box
|
||||
|
||||
When designing a dialog box, follow these suggestions to create a good user experience:
|
||||
|
||||
❌ DON'T clutter the dialog window. The dialog experience is for the user to enter some data, or to make a choice.
|
||||
|
||||
✔️ DO provide an **OK** button to close the window.
|
||||
|
||||
✔️ DO set the **OK** button's <xref:System.Windows.Controls.Button.IsDefault%2A> property to `true` to allow the user to press the <kbd>ENTER</kbd> key to accept and close the window.
|
||||
|
||||
✔️ CONSIDER adding a **Cancel** button so that the user can close the window and indicate that they don't want to continue.
|
||||
|
||||
✔️ DO set the **Cancel** button's <xref:System.Windows.Controls.Button.IsCancel%2A> property to `true` to allow the user to press the <kbd>ESC</kbd> key to close the window.
|
||||
|
||||
✔️ DO set the title of the window to accurately describe what the dialog represents, or what the user should do with the dialog.
|
||||
|
||||
✔️ DO set minimum width and height values for the window, preventing the user from resizing the window too small.
|
||||
|
||||
✔️ CONSIDER disabling the ability to resize the window if <xref:System.Windows.Window.ShowInTaskbar%2A> is set to `false`. You can disable resizing by setting <xref:System.Windows.Window.ResizeMode%2A> to <xref:System.Windows.ResizeMode.NoResize>
|
||||
|
||||
The following code demonstrates this configuration.
|
||||
|
||||
:::code language="xaml" source="snippets/dialog-boxes-overview/csharp/Margins.xaml" highlight="5-9,58-59":::
|
||||
|
||||
The above XAML creates a window that looks similar to the following image:
|
||||
|
||||
:::image type="content" source="media/dialog-boxes-overview/example-dialog.png" alt-text="A dialog box window for WPF that shows left, top, right, bottom text boxes.":::
|
||||
|
||||
## UI elements opening a dialog box
|
||||
|
||||
The user experience for a dialog box also extends into the menu bar or the button of the window that opens it. When a menu item or button runs a function that requires user interaction through a dialog box before the function can continue, the control should use an ellipsis at the end of its header text:
|
||||
|
||||
```xaml
|
||||
<MenuItem Header="_Margins..." Click="formatMarginsMenuItem_Click" />
|
||||
<!-- or -->
|
||||
<Button Content="_Margins..." Click="formatMarginsButton_Click" />
|
||||
```
|
||||
|
||||
When a menu item or button runs a function that displays a dialog box that **doesn't** require user interaction, such as an _About_ dialog box, an ellipsis isn't required.
|
||||
|
||||
### Menu items
|
||||
|
||||
Menu items are a common way to provide users with application actions that are grouped into related themes. You've probably seen the _File_ menu on many different applications. In a typical application, the _File_ menu item provides ways to save a file, load a file, and print a file. If the action is going to display a modal window, the header typically includes an ellipsis as shown in the following image:
|
||||
|
||||
:::image type="content" source="media/dialog-boxes-overview/simple-text-editor-menu.png" alt-text="A WPF window that shows menu items with an ellipsis to indicate which item shows a dialog box.":::
|
||||
|
||||
Two of the menu items have an ellipsis: `...`. This helps the user identify that when they select those menu items, a modal window is shown, pausing the application until the user closes it.
|
||||
|
||||
This design technique is an easy way for you to communicate to your users what they should expect.
|
||||
|
||||
### Buttons
|
||||
|
||||
You can follow the same principle described in the [Menu items](#menu-items) section. Use an ellipsis on the button text to indicate that when the user presses the button, a modal dialog will appear. In the following image, there are two buttons and it's easy to understand which button displays a dialog box:
|
||||
|
||||
:::image type="content" source="media/dialog-boxes-overview/simple-text-editor.png" alt-text="A WPF window that shows buttons with an ellipsis to indicate which item shows a dialog box.":::
|
||||
|
||||
## Return a result
|
||||
|
||||
Opening another window, especially a modal dialog box, is a great way to return status and information to calling code.
|
||||
|
||||
### Modal dialogs
|
||||
|
||||
When a dialog box is shown by calling <xref:System.Windows.Window.ShowDialog>, the code that opened the dialog box waits until the `ShowDialog` method returns. When the method returns, the code that called it needs to decide whether to continue processing or stop processing. The user generally indicates this by pressing an **OK** or **Cancel** button on the dialog box.
|
||||
|
||||
When the **OK** button is pressed, `ShowDialog` should be designed to return `true`, and the **Cancel** button to return `false`. This is achieved by setting the <xref:System.Windows.Window.DialogResult> property when the button is pressed.
|
||||
|
||||
:::code language="csharp" source="snippets/dialog-boxes-overview/csharp/Window1.xaml.cs" id="DialogResultButtons":::
|
||||
:::code language="vb" source="snippets/dialog-boxes-overview/vb/Window1.xaml.vb" id="DialogResultButtons":::
|
||||
|
||||
The <xref:System.Windows.Window.DialogResult> property can only be set if the dialog box was displayed with <xref:System.Windows.Window.ShowDialog>. When the `DialogResult` property is set, the dialog box closes.
|
||||
|
||||
If a button's <xref:System.Windows.Controls.Button.IsCancel%2A> property is set to `true`, and the window is opened with <xref:System.Windows.Window.ShowDialog>, the <kbd>ESC</kbd> key will close the window and set `DialogResult` to `false`.
|
||||
|
||||
For more information about closing dialog boxes, see [How to close a window or dialog box](how-to-close-window-dialog-box.md).
|
||||
|
||||
#### Processing the response
|
||||
|
||||
The <xref:System.Windows.Window.ShowDialog> returns a boolean value to indicate whether the user accepted or canceled the dialog box. If you're alerting the user to something, but not requiring they make a decision or provide data, you can ignore the response. The response can also be inspected by checking the <xref:System.Windows.Window.DialogResult> property. The following code shows how to process the response:
|
||||
|
||||
:::code language="csharp" source="snippets/dialog-boxes-overview/csharp/Window1.xaml.cs" id="HandleDialogResponse":::
|
||||
:::code language="vb" source="snippets/dialog-boxes-overview/vb/Window1.xaml.vb" id="HandleDialogResponse":::
|
||||
|
||||
### Modeless dialog
|
||||
|
||||
To show a dialog box modeless, call <xref:System.Windows.Window.Show>. The dialog box should at least provide a **Close** button. Other buttons and interactive elements can be provided to run a specific function, such as a **Find Next** button to find the next word in a word search.
|
||||
|
||||
Because a modeless dialog box doesn't block the calling code from continuing, you must provide a different way of returning a result. You can do one of the following:
|
||||
|
||||
- Expose a data object property on the window.
|
||||
- Handle the <xref:System.Windows.Window.Closed?displayProperty=nameWithType> event in the calling code.
|
||||
- Create events on the window that are raised when the user selects an object or presses a specific button.
|
||||
|
||||
The following example uses the <xref:System.Windows.Window.Closed?displayProperty=nameWithType> event to display a message box to the user when the dialog box closes. The message displayed references a property of the closed dialog box. For more information about closing dialogs boxes, see [How to close a window or dialog box](how-to-close-window-dialog-box.md).
|
||||
|
||||
:::code language="csharp" source="snippets/dialog-boxes-overview/csharp/Window1.xaml.cs" id="HandleClosing":::
|
||||
:::code language="vb" source="snippets/dialog-boxes-overview/vb/Window1.xaml.vb" id="HandleClosing":::
|
||||
|
||||
## See also
|
||||
|
||||
- [Overview of WPF windows](index.md)
|
||||
- [How to open a window or dialog box](how-to-open-window-dialog-box.md)
|
||||
- [How to open a common dialog box](how-to-open-common-system-dialog-box.md)
|
||||
- [How to open a message box](how-to-open-message-box.md)
|
||||
- [How to close a window or dialog box](how-to-close-window-dialog-box.md)
|
||||
- [Dialog Box Sample](https://github.com/Microsoft/WPF-Samples/tree/master/Windows/DialogBox)
|
||||
- <xref:System.Windows.Window?displayProperty=fullName>
|
||||
- <xref:System.Windows.MessageBox?displayProperty=fullName>
|
||||
@@ -0,0 +1,91 @@
|
||||
---
|
||||
title: How to close a window
|
||||
description: Learn about how to close a window or dialog box in Windows Foundation Presentation (WPF). You'll also learn how to return a result from the window or dialog box.
|
||||
ms.date: "03/24/2021"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "modeless dialog boxes [WPF]"
|
||||
- "modal dialog boxes [WPF]"
|
||||
- "dialog boxes [WPF]"
|
||||
- "message boxes [WPF]"
|
||||
---
|
||||
|
||||
# How to close a window or dialog box (WPF .NET)
|
||||
|
||||
In this article, you'll learn about the different ways to close a window or dialog box. A user can close a window by using the elements in the non-client area, including the following:
|
||||
|
||||
- The **Close** item of the **System** menu.
|
||||
- Pressing <kbd>ALT + F4</kbd>.
|
||||
- Pressing the **Close** button.
|
||||
- Pressing <kbd>ESC</kbd> when a button has the <xref:System.Windows.Controls.Button.IsCancel%2A> property set to `true` on a modal window.
|
||||
|
||||
When designing a window, provide more mechanisms to the client area to close a window. Some of the common design elements on a window that are used to close it include the following:
|
||||
|
||||
- An **Exit** item in the **File** menu, typically for main application windows.
|
||||
- A **Close** item in the **File** menu, typically on a secondary application window.
|
||||
- A **Cancel** button, typically on a modal dialog box.
|
||||
- A **Close** button, typically on a modeless dialog box.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Once a window is closed, the same object instance can't be used to reopen the window.
|
||||
|
||||
For more information about the life of a window, see [Overview of WPF windows: Window lifetime](index.md#window-lifetime).
|
||||
|
||||
## Close a modal window
|
||||
|
||||
When closing a window that was opened with the <xref:System.Windows.Window.ShowDialog%2A> method, set the <xref:System.Windows.Window.DialogResult> property to either `true` or `false` to indicate an "accepted" or "canceled" state, respectively. As soon as the `DialogResult` property is set to a value, the window closes. The following code demonstrates setting the `DialogResult` property:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-close-window-dialog-box/csharp/Menus.xaml.cs" id="CloseDialog":::
|
||||
:::code language="vb" source="snippets/how-to-close-window-dialog-box/vb/Menus.xaml.vb" id="CloseDialog":::
|
||||
|
||||
You can also call the <xref:System.Windows.Window.Close%2A> method. If the `Close` method is used, the <xref:System.Windows.Window.DialogResult> property is set to `false`.
|
||||
|
||||
Once a window has been closed, it can't be reopened with the same object instance. If you try to show the same window, a <xref:System.InvalidOperationException> is thrown. Instead, create a new instance of the window and open it.
|
||||
|
||||
## Close a modeless window
|
||||
|
||||
When closing a window that was opened with the <xref:System.Windows.Window.Show%2A> method, use the <xref:System.Windows.Window.Close%2A> method. The following code demonstrates closing a modeless window:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-close-window-dialog-box/csharp/Menus.xaml.cs" id="CloseNormal":::
|
||||
:::code language="vb" source="snippets/how-to-close-window-dialog-box/vb/Menus.xaml.vb" id="CloseNormal":::
|
||||
|
||||
## Close with IsCancel
|
||||
|
||||
The <xref:System.Windows.Controls.Button.IsCancel?displayProperty=nameWithType> property can be set to `true` to enable the <kbd>ESC</kbd> key to automatically close the window. This only works when the window is opened with <xref:System.Windows.Window.ShowDialog%2A> method.
|
||||
|
||||
:::code language="xaml" source="snippets/how-to-close-window-dialog-box/csharp/Window1.xaml" id="CancelButton":::
|
||||
|
||||
## Hide a window
|
||||
|
||||
Instead of closing a window, a window can be hidden with the <xref:System.Windows.Window.Hide%2A> method. A hidden window can be reopened, unlike a window that has been closed. If you're going to reuse a window object instance, hide the window instead of closing it. The following code demonstrates hiding a window:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-close-window-dialog-box/csharp/Menus.xaml.cs" id="Hide":::
|
||||
:::code language="vb" source="snippets/how-to-close-window-dialog-box/vb/Menus.xaml.vb" id="Hide":::
|
||||
|
||||
## Cancel close and hide
|
||||
|
||||
If you've designed your buttons to hide a window instead of closing it, the user can still bypass this and close the window. The **Close** item of the system menu and the **Close** button of the non-client area of the window, will close the window instead of hiding it. Consider this scenario when your intent is to hide a window instead of closing it.
|
||||
|
||||
> [!CAUTION]
|
||||
> If a window is shown modally with <xref:System.Windows.Window.ShowDialog%2A>, the <xref:System.Windows.Window.DialogResult> property will be set to `null` when the window is hidden. You'll need to communicate state back to the calling code by adding your own property to the window.
|
||||
|
||||
When a window is closed, the <xref:System.Windows.Window.Closing> event is raised. The handler is passed a <xref:System.ComponentModel.CancelEventArgs>, which implements the <xref:System.ComponentModel.CancelEventArgs.Cancel> property. Set that property to `true` to prevent a window from closing. The following code demonstrates how to cancel the closure and instead hide the window:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-close-window-dialog-box/csharp/Margins.xaml.cs" id="CancelAndHide":::
|
||||
:::code language="vb" source="snippets/how-to-close-window-dialog-box/vb/Margins.xaml.vb" id="CancelAndHide":::
|
||||
|
||||
There may be times where you don't want to hide a window, but actually prevent the user from closing it. For more information, see [Overview of WPF windows: Cancel window closure](index.md#cancel-window-closure).
|
||||
|
||||
## See also
|
||||
|
||||
- [Overview of WPF windows](index.md)
|
||||
- [Dialog boxes overview](dialog-boxes-overview.md)
|
||||
- [How to open a window or dialog box](how-to-open-window-dialog-box.md)
|
||||
- <xref:System.Windows.Window.Close?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.Closing?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.DialogResult?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.Hide?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.Show?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.ShowDialog?displayProperty=fullName>
|
||||
@@ -0,0 +1,72 @@
|
||||
---
|
||||
title: How to get or set main app window
|
||||
description: Learn about how to get and set the main application window for a Windows Presentation Foundation (WPF) application. The main window of an application is typically the startup window. This is sometimes specified by the StartupUri property.
|
||||
ms.date: "03/24/2021"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "windows objects [WPF], setting"
|
||||
- "setting windows objects [WPF]"
|
||||
- "windows objects [WPF], getting"
|
||||
- "getting windows objects [WPF]"
|
||||
---
|
||||
|
||||
# How to get or set the main application window (WPF .NET)
|
||||
|
||||
This article teaches you how to get or set the main application window for Windows Presentation Foundation (WPF). The first <xref:System.Windows.Window> that is instantiated within a WPF application is automatically set by <xref:System.Windows.Application> as the main application window. The main window is referenced with the <xref:System.Windows.Application.MainWindow%2A?displayProperty=nameWithType> property.
|
||||
|
||||
Much of the time a project template will set the <xref:System.Windows.Application.StartupUri%2A?displayProperty=nameWithType> to a XAML file within your application, such as :::no-loc text="_Window1.xaml_":::. This is the first window instantiated and shown by your application, and it becomes the main window.
|
||||
|
||||
> [!TIP]
|
||||
>The default behavior for an application is to shutdown when the last window is closed. This behavior is controlled by the <xref:System.Windows.Application.ShutdownMode%2A?displayProperty=nameWithType> property. Instead, you can configure the application to shutdown if the <xref:System.Windows.Application.MainWindow%2A> is closed. Set <xref:System.Windows.Application.ShutdownMode%2A?displayProperty=nameWithType> to <xref:System.Windows.ShutdownMode.OnMainWindowClose> to enable this behavior.
|
||||
|
||||
## Set the main window in XAML
|
||||
|
||||
The templates that generate your WPF application typically set the <xref:System.Windows.Application.StartupUri%2A?displayProperty=nameWithType> property to a XAML file. This property is helpful because:
|
||||
|
||||
01. It's easily changeable to a different XAML file in your project.
|
||||
01. Automatically instantiates and displays the specified window.
|
||||
01. The specified window becomes the <xref:System.Windows.Application.MainWindow%2A?displayProperty=nameWithType>.
|
||||
|
||||
:::code language="xaml" source="snippets/how-to-get-set-main-application-window/csharp/App.xaml" highlight="5" :::
|
||||
|
||||
Instead of using <xref:System.Windows.Application.StartupUri%2A?displayProperty=nameWithType>, you can set the <xref:System.Windows.Application.MainWindow%2A?displayProperty=nameWithType> to a XAML-declared window. However, the window specified here won't be displayed and you must set its visibility.
|
||||
|
||||
:::code language="xaml" source="snippets/how-to-get-set-main-application-window_2/csharp/App.xaml" highlight="6-8" :::
|
||||
|
||||
> [!CAUTION]
|
||||
> If you set both the <xref:System.Windows.Application.StartupUri%2A?displayProperty=nameWithType> and the <xref:System.Windows.Application.MainWindow%2A?displayProperty=nameWithType> properties, you'll display both windows when your application starts.
|
||||
|
||||
Also, you can use the <xref:System.Windows.Application.Startup?displayProperty=nameWithType> event to open a window. For more information, see [Use the Startup event to open a window](index.md#opening-a-window).
|
||||
|
||||
## Set the main window in code
|
||||
|
||||
The first window instantiated by your application automatically becomes the main window and is set to the <xref:System.Windows.Application.MainWindow%2A?displayProperty=nameWithType> property. To set a different main window, change this property to a window:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-get-set-main-application-window/csharp/App.xaml.cs" id="MainWindowDirect":::
|
||||
:::code language="vb" source="snippets/how-to-get-set-main-application-window/vb/Application.xaml.vb" id="MainWindowDirect":::
|
||||
|
||||
If your application has never created an instance of a window, the following code is functionally equivalent to the previous code:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-get-set-main-application-window/csharp/App.xaml.cs" id="MainWindowIndirect":::
|
||||
:::code language="vb" source="snippets/how-to-get-set-main-application-window/vb/Application.xaml.vb" id="MainWindowIndirect":::
|
||||
|
||||
As soon as the window object instance is created, it's assigned to <xref:System.Windows.Application.MainWindow%2A?displayProperty=nameWithType>.
|
||||
|
||||
## Get the main window
|
||||
|
||||
You can access the window chosen as the main window by inspecting the <xref:System.Windows.Application.MainWindow%2A?displayProperty=nameWithType> property. The following code displays a message box with the title of the main window when a button is clicked:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-get-set-main-application-window/csharp/Window1.xaml.cs" id="GetMainWindow":::
|
||||
:::code language="vb" source="snippets/how-to-get-set-main-application-window/vb/Window1.xaml.vb" id="GetMainWindow":::
|
||||
|
||||
## See also
|
||||
|
||||
- [Overview of WPF windows](index.md)
|
||||
- [Use the Startup event to open a window](index.md#opening-a-window)
|
||||
- [How to open a window or dialog box](how-to-open-window-dialog-box.md)
|
||||
- <xref:System.Windows.Application?displayProperty=fullName>
|
||||
- <xref:System.Windows.Application.MainWindow?displayProperty=fullName>
|
||||
- <xref:System.Windows.Application.StartupUri?displayProperty=fullName>
|
||||
- <xref:System.Windows.Application.ShutdownMode?displayProperty=fullName>
|
||||
@@ -0,0 +1,68 @@
|
||||
---
|
||||
title: How to open a common dialog box
|
||||
description: Learn about how to show a system dialog box in Windows Foundation Presentation (WPF). System dialog boxes prompt users for information, choosing a file to load or save, or displaying the printer window.
|
||||
ms.date: 03/15/2021
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "system dialog boxes [WPF]"
|
||||
- "dialog boxes [WPF]"
|
||||
- "modal dialog boxes [WPF]"
|
||||
---
|
||||
|
||||
# How to open a common dialog box (WPF .NET)
|
||||
|
||||
This article demonstrates how you can display a common system dialog box in Windows Presentation Foundation (WPF). Windows implements different kinds of reusable dialog boxes that are common to all applications, including dialog boxes for selecting files and printing.
|
||||
|
||||
Since these dialog boxes are provided by the operating system, they're shared among all the applications that run on the operating system. These dialog boxes provide a consistent user experience, and are known as *common dialog boxes*. As a user uses a common dialog box in one application, they don't need to learn how to use that dialog box in other applications.
|
||||
|
||||
A message box is another common dialog box. For more information, see [How to open a message box](how-to-open-message-box.md).
|
||||
|
||||
## Open File dialog box
|
||||
|
||||
The open file dialog box is used by file opening functionality to retrieve the name of a file to open.
|
||||
|
||||
:::image type="content" source="media/how-to-open-common-system-dialog-box/open-file-dialog-box.png" alt-text="An Open dialog box showing the location to retrieve the file shown from a WPF application.":::
|
||||
|
||||
The common open file dialog box is implemented as the <xref:Microsoft.Win32.OpenFileDialog> class and is located in the <xref:Microsoft.Win32> namespace. The following code shows how to create, configure, and show one, and how to process the result.
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-common-system-dialog-box/csharp/Window1.xaml.cs" id="OpenFile":::
|
||||
:::code language="vb" source="snippets/how-to-open-common-system-dialog-box/vb/Window1.xaml.vb" id="OpenFile":::
|
||||
|
||||
For more information on the open file dialog box, see <xref:Microsoft.Win32.OpenFileDialog?displayProperty=nameWithType>.
|
||||
|
||||
## Save File dialog box
|
||||
|
||||
The save file dialog box is used by file saving functionality to retrieve the name of a file to save.
|
||||
|
||||
:::image type="content" source="media/how-to-open-common-system-dialog-box/save-file-dialog-box.png" alt-text="A Save As dialog box showing the location to save the file shown from a WPF application.":::
|
||||
|
||||
The common save file dialog box is implemented as the <xref:Microsoft.Win32.SaveFileDialog> class, and is located in the <xref:Microsoft.Win32> namespace. The following code shows how to create, configure, and show one, and how to process the result.
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-common-system-dialog-box/csharp/Window1.xaml.cs" id="SaveFile":::
|
||||
:::code language="vb" source="snippets/how-to-open-common-system-dialog-box/vb/Window1.xaml.vb" id="SaveFile":::
|
||||
|
||||
For more information on the save file dialog box, see <xref:Microsoft.Win32.SaveFileDialog?displayProperty=nameWithType>.
|
||||
|
||||
## Print dialog box
|
||||
|
||||
The print dialog box is used by printing functionality to choose and configure the printer that a user wants to print data to.
|
||||
|
||||
:::image type="content" source="media/how-to-open-common-system-dialog-box/print-data-dialog-box.png" alt-text="A print dialog box shown from a WPF application.":::
|
||||
|
||||
The common print dialog box is implemented as the <xref:System.Windows.Controls.PrintDialog> class, and is located in the <xref:System.Windows.Controls> namespace. The following code shows how to create, configure, and show one.
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-common-system-dialog-box/csharp/Window1.xaml.cs" id="Print":::
|
||||
:::code language="vb" source="snippets/how-to-open-common-system-dialog-box/vb/Window1.xaml.vb" id="Print":::
|
||||
|
||||
For more information on the print dialog box, see <xref:System.Windows.Controls.PrintDialog?displayProperty=nameWithType>. For detailed discussion of printing in WPF, see [Printing overview](../../../framework/wpf/advanced/printing-overview.md).
|
||||
|
||||
## See also
|
||||
|
||||
- [How to open a message box](how-to-open-message-box.md)
|
||||
- [Dialog boxes overview](dialog-boxes-overview.md)
|
||||
- [Overview of WPF windows](index.md)
|
||||
- <xref:Microsoft.Win32.OpenFileDialog?displayProperty=fullName>
|
||||
- <xref:Microsoft.Win32.SaveFileDialog?displayProperty=fullName>
|
||||
- <xref:System.Windows.Controls.PrintDialog?displayProperty=fullName>
|
||||
@@ -0,0 +1,107 @@
|
||||
---
|
||||
title: How to display a message box
|
||||
description: Learn about how to show a message box in Windows Foundation Presentation (WPF). Message boxes prompt users for a response, allowing the calling window to process that response.
|
||||
ms.date: 03/15/2021
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "message boxes [WPF]"
|
||||
---
|
||||
|
||||
# How to open a message box (WPF .NET)
|
||||
|
||||
A _message box_ is a dialog box that is used to quickly display information and optionally allow users to make decisions. Access to the message box is provided by the <xref:System.Windows.MessageBox> class. A message box is displayed _modally_. And the code that displays the message box is paused until the user closes the message box either with the close button or a response button.
|
||||
|
||||
The following illustration demonstrates the parts of a message box:
|
||||
|
||||
<a name="diagram"></a>
|
||||
:::image type="content" source="media/how-to-open-message-box/diagram.png" alt-text="A figure that shows the parts of a message box for WPF.":::
|
||||
|
||||
- A title bar with a caption (1).
|
||||
- A close button (2).
|
||||
- Icon (3).
|
||||
- Message displayed to the user (4).
|
||||
- Response buttons (5).
|
||||
|
||||
For presenting or gathering complex data, a dialog box might be more suitable than a message box. For more information, see [Dialog boxes overview](dialog-boxes-overview.md).
|
||||
|
||||
## Display a message box
|
||||
|
||||
To create a message box, you use the <xref:System.Windows.MessageBox> class. The <xref:System.Windows.MessageBox.Show%2A?displayProperty=nameWithType> method lets you configure the message box text, title, icon, and buttons, shown in the following code:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="YesNoCancel":::
|
||||
:::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="YesNoCancel":::
|
||||
|
||||
The <xref:System.Windows.MessageBox.Show%2A?displayProperty=nameWithType> method overloads provide ways to configure the message box. These options include:
|
||||
|
||||
- Title bar **caption**
|
||||
- Message **icon**
|
||||
- Message **text**
|
||||
- Response **buttons**
|
||||
|
||||
Here are some more examples of using a message box.
|
||||
|
||||
- Display an alert.
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="AlertSimple":::
|
||||
:::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="AlertSimple":::
|
||||
|
||||
The previous code displays a message box like the following image:
|
||||
|
||||
:::image type="content" source="media/how-to-open-message-box/alert-simple.png" alt-text="A simple message box for WPF that has no options configured.":::
|
||||
|
||||
It's a good idea to use the options provided by the message box class. Using the same alert as before, set more options to make it more visually appealing:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="Alert":::
|
||||
:::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="Alert":::
|
||||
|
||||
The previous code displays a message box like the following image:
|
||||
|
||||
:::image type="content" source="media/how-to-open-message-box/alert.png" alt-text="A warning message box for WPF that has an icon, caption, and text.":::
|
||||
|
||||
- Display a warning.
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="Warning":::
|
||||
:::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="Warning":::
|
||||
|
||||
The previous code displays a message box like the following image:
|
||||
|
||||
:::image type="content" source="media/how-to-open-message-box/warning.png" alt-text="A simple message box for WPF that has displays a warning icon.":::
|
||||
|
||||
- Ask the user a question.
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="Prompt":::
|
||||
:::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="Prompt":::
|
||||
|
||||
The previous code displays a message box like the following image:
|
||||
|
||||
:::image type="content" source="media/how-to-open-message-box/prompt.png" alt-text="A simple message box for WPF that prompts the user with a yes or no question.":::
|
||||
|
||||
## Handle a message box response
|
||||
|
||||
The <xref:System.Windows.MessageBox.Show%2A?displayProperty=nameWithType> method displays the message box and returns a result. The result indicates how the user closed the message box:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-message-box/csharp/Window1.xaml.cs" id="MessageBoxShow":::
|
||||
:::code language="vb" source="snippets/how-to-open-message-box/vb/Window1.xaml.vb" id="MessageBoxShow":::
|
||||
|
||||
When a user presses the buttons at the bottom of the message box, the corresponding <xref:System.Windows.MessageBoxResult> is returned. However, if the user presses the <kbd>ESC</kbd> key or presses the **Close** button (#2 in the [message box illustration](#diagram)), the result of the message box varies based on the button options:
|
||||
|
||||
| Button options | <kbd>ESC</kbd> or **Close** button result |
|
||||
|----------------|-------------------------------------------|
|
||||
| `Ok` | `Ok` |
|
||||
| `OkCancel` | `Cancel` |
|
||||
| `YesNo` | <kbd>ESC</kbd> keyboard shortcut and **Close** button disabled. User must press **Yes** or **No**. |
|
||||
| `YesNoCancel` | `Cancel` |
|
||||
|
||||
For more information on using message boxes, see <xref:System.Windows.MessageBox> and the [MessageBox sample](https://github.com/Microsoft/WPF-Samples/tree/master/Windows/MessageBox).
|
||||
|
||||
## See also
|
||||
|
||||
- [Overview of WPF windows](index.md)
|
||||
- [Dialog boxes overview](dialog-boxes-overview.md)
|
||||
- [How to display a common dialog box](how-to-open-common-system-dialog-box.md)
|
||||
- [MessageBox sample](https://github.com/Microsoft/WPF-Samples/tree/master/Windows/MessageBox)
|
||||
- <xref:System.Windows.MessageBox?displayProperty=fullName>
|
||||
- <xref:System.Windows.MessageBox.Show%2A?displayProperty=fullName>
|
||||
- <xref:System.Windows.MessageBoxResult?displayProperty=fullName>
|
||||
@@ -0,0 +1,59 @@
|
||||
---
|
||||
title: How to open a window
|
||||
description: Learn about how to show a window or dialog box in Windows Foundation Presentation (WPF). You'll also learn how to handle common scenarios around managing a window or dialog box.
|
||||
ms.date: "03/12/2021"
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "modeless dialog boxes [WPF]"
|
||||
- "modal dialog boxes [WPF]"
|
||||
- "dialog boxes [WPF]"
|
||||
- "message boxes [WPF]"
|
||||
---
|
||||
|
||||
# How to open a window or dialog box (WPF .NET)
|
||||
|
||||
You can create your own windows and display them in Windows Presentation Foundation (WPF). In this article, you'll learn how to display modal and modeless windows and dialogs.
|
||||
|
||||
Conceptually, a window and a dialog box are the same thing: they're displayed to a user to provide information or interaction. They're both "window" objects. The design of the window and the way it's used, is what makes a dialog box. A dialog box is generally small in size and requires the user to respond to it. For more information, see [Overview of WPF windows](index.md) and [Dialog boxes overview](dialog-boxes-overview.md).
|
||||
|
||||
If you're interested in opening operating system dialog boxes, see [How to open a common dialog box](how-to-open-common-system-dialog-box.md).
|
||||
|
||||
## Open as modal
|
||||
|
||||
When a modal window is opened, it generally represents a dialog box. WPF restricts interaction to the modal window, and the code that opened the window pauses until the window closes. This mechanism provides an easy way for you to prompt the user with data and wait for their response.
|
||||
|
||||
Use the <xref:System.Windows.Window.ShowDialog%2A> method to open a window. The following code instantiates the window, and opens it modally. The code opening the window pauses, waiting for the window to be closed:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-window-dialog-box/csharp/Window1.xaml.cs" id="ShowDialog":::
|
||||
:::code language="vb" source="snippets/how-to-open-window-dialog-box/vb/Window1.xaml.vb" id="ShowDialog":::
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Once a window is closed, the same object instance can't be used to reopen the window.
|
||||
|
||||
For more information about how to handle the user response to a dialog box, see [Dialog boxes overview: Processing the response](dialog-boxes-overview.md#processing-the-response).
|
||||
|
||||
## Open as modeless
|
||||
|
||||
Opening a window modeless means displaying it as a normal window. The code that opens the window continues to run as the window becomes visible. You can focus and interact with all modeless windows displayed by your application, without restriction.
|
||||
|
||||
Use the <xref:System.Windows.Window.Show%2A> method to open a window. The following code instantiates the window, and opens it modeless. The code opening the window continues to run:
|
||||
|
||||
:::code language="csharp" source="snippets/how-to-open-window-dialog-box/csharp/Window1.xaml.cs" id="ShowNormal":::
|
||||
:::code language="vb" source="snippets/how-to-open-window-dialog-box/vb/Window1.xaml.vb" id="ShowNormal":::
|
||||
|
||||
> [!IMPORTANT]
|
||||
> Once a window is closed, the same object instance can't be used to reopen the window.
|
||||
|
||||
## See also
|
||||
|
||||
- [Overview of WPF windows](index.md)
|
||||
- [Dialog boxes overview](dialog-boxes-overview.md)
|
||||
- [How to close a window or dialog box](how-to-close-window-dialog-box.md)
|
||||
- [How to open a common dialog box](how-to-open-common-system-dialog-box.md)
|
||||
- [How to open a message box](how-to-open-message-box.md)
|
||||
- <xref:System.Windows.Window?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.DialogResult?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.Show?displayProperty=fullName>
|
||||
- <xref:System.Windows.Window.ShowDialog?displayProperty=fullName>
|
||||
@@ -0,0 +1,592 @@
|
||||
---
|
||||
title: Windows in WPF overview
|
||||
description: Learn about the basics of how Window objects work in WPF. Learn how to create and manage a window for a Windows Presentation Foundation (WPF) app.
|
||||
ms.date: "03/11/2021"
|
||||
ms.topic: overview
|
||||
dev_langs:
|
||||
- "csharp"
|
||||
- "vb"
|
||||
helpviewer_keywords:
|
||||
- "XAML [WPF], displaying content via"
|
||||
- "XAML pages [WPF], displaying"
|
||||
- "content [WPF], displaying via XAML"
|
||||
- "window objects [WPF]"
|
||||
- "hosting [WPF], applications"
|
||||
- "managing windows [WPF]"
|
||||
- "dialog boxes [WPF]"
|
||||
- "Page object [WPF]"
|
||||
- "NavigationWindow objects [WPF]"
|
||||
- "applications [WPF], hosting"
|
||||
- "content [WPF], displaying"
|
||||
- "events [WPF]"
|
||||
- "content [WPF], displaying via procedural code"
|
||||
- "modal windows [WPF]"
|
||||
- "procedural code [WPF], displaying content via"
|
||||
- "displaying content via procedural code [WPF]"
|
||||
- "window management [WPF]"
|
||||
- "displaying content [WPF]"
|
||||
- "window events [WPF]"
|
||||
- "windows [WPF]"
|
||||
- "modal dialog boxes [WPF]"
|
||||
- "displaying XAML pages [WPF]"
|
||||
---
|
||||
|
||||
# Overview of WPF windows (WPF .NET)
|
||||
|
||||
Users interact with Windows Presentation Foundation (WPF) applications through windows. The primary purpose of a window is to host content that visualizes data and enables users to interact with data. WPF applications provide their own windows by using the <xref:System.Windows.Window> class. This article introduces <xref:System.Windows.Window> before covering the fundamentals of creating and managing windows in applications.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> This article uses XAML generated from a **C#** project. If you're using **Visual Basic**, the XAML may look slightly different. These differences are typically present on `x:Class` attribute values. C# includes the root namespace for the project while Visual Basic doesn't.
|
||||
>
|
||||
> The project templates for C# create an `App` type contained in the _app.xaml_ file. In Visual Basic, the type is named `Application` and the file is named `Application.xaml`.
|
||||
|
||||
## The Window class
|
||||
|
||||
In WPF, a window is encapsulated by the <xref:System.Windows.Window> class that you use to do the following:
|
||||
|
||||
- Display a window.
|
||||
- Configure the size, position, and appearance of a window.
|
||||
- Host application-specific content.
|
||||
- Manage the lifetime of a window.
|
||||
|
||||
The following figure illustrates the constituent parts of a window:
|
||||
|
||||
:::image type="content" source="./media/index/window-parts.png" alt-text="Screenshot that shows parts of a WPF window.":::
|
||||
|
||||
A window is divided into two areas: the non-client area and client area.
|
||||
|
||||
The *non-client area* of a window is implemented by WPF and includes the parts of a window that are common to most windows, including the following:
|
||||
|
||||
- A title bar (1-5).
|
||||
- An icon (1).
|
||||
- Title (2).
|
||||
- Minimize (3), Maximize (4), and Close (5) buttons.
|
||||
- System menu (6) with menu items. Appears when clicking on the icon (1).
|
||||
- Border (7).
|
||||
|
||||
The *client area* of a window is the area within a window's non-client area and is used by developers to add application-specific content, such as menu bars, tool bars, and controls.
|
||||
|
||||
- Client area (8).
|
||||
- Resize grip (9). This is a control added to the Client area (8).
|
||||
|
||||
## Implementing a window
|
||||
|
||||
The implementation of a typical window includes both appearance and behavior, where *appearance* defines how a window looks to users and *behavior* defines the way a window functions as users interact with it. In WPF, you can implement the appearance and behavior of a window using either code or XAML markup.
|
||||
|
||||
In general, however, the appearance of a window is implemented using XAML markup, and its behavior is implemented using code-behind, as shown in the following example.
|
||||
|
||||
```xaml
|
||||
<Window x:Class="WindowsOverview.Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:WindowsOverview"
|
||||
>
|
||||
|
||||
<!-- Client area containing the content of the window -->
|
||||
|
||||
</Window>
|
||||
```
|
||||
|
||||
The following code is the code-behind for the XAML.
|
||||
|
||||
```csharp
|
||||
using System.Windows;
|
||||
|
||||
namespace WindowsOverview
|
||||
{
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```vb
|
||||
Public Class Window1
|
||||
|
||||
End Class
|
||||
```
|
||||
|
||||
To enable a XAML markup file and code-behind file to work together, the following are required:
|
||||
|
||||
- In markup, the `Window` element must include the `x:Class` attribute. When the application is built, the existence of `x:Class` attribute causes Microsoft build engine (MSBuild) to generate a `partial` class that derives from <xref:System.Windows.Window> with the name that is specified by the `x:Class` attribute. This requires the addition of an XML namespace declaration for the XAML schema (`xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"`). The generated `partial` class implements the `InitializeComponent` method, which is called to register the events and set the properties that are implemented in markup.
|
||||
|
||||
- In code-behind, the class must be a `partial` class with the same name that is specified by the `x:Class` attribute in markup, and it must derive from <xref:System.Windows.Window>. This allows the code-behind file to be associated with the `partial` class that is generated for the markup file when the application is built, for more information, see [Compile a WPF Application](../../../framework/wpf/app-development/building-a-wpf-application-wpf.md).
|
||||
|
||||
- In code-behind, the <xref:System.Windows.Window> class must implement a constructor that calls the `InitializeComponent` method. `InitializeComponent` is implemented by the markup file's generated `partial` class to register events and set properties that are defined in markup.
|
||||
|
||||
> [!NOTE]
|
||||
> When you add a new <xref:System.Windows.Window> to your project by using Visual Studio, the <xref:System.Windows.Window> is implemented using both markup and code-behind, and includes the necessary configuration to create the association between the markup and code-behind files as described here.
|
||||
|
||||
With this configuration in place, you can focus on defining the appearance of the window in XAML markup and implementing its behavior in code-behind. The following example shows a window with a button that defines an event handler for the <xref:System.Windows.Controls.Primitives.ButtonBase.Click> event. This is implemented in the XAML and the handler is implemented in code-behind.
|
||||
|
||||
```xaml
|
||||
<Window x:Class="WindowsOverview.Final"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:WindowsOverview"
|
||||
>
|
||||
|
||||
<!-- Client area containing the content of the window -->
|
||||
|
||||
<Button Click="Button_Click">Click This Button</Button>
|
||||
|
||||
</Window>
|
||||
```
|
||||
|
||||
The following code is the code-behind for the XAML.
|
||||
|
||||
```csharp
|
||||
using System.Windows;
|
||||
|
||||
namespace WindowsOverview
|
||||
{
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
MessageBox.Show("Button was clicked.");
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```vb
|
||||
Public Class Window1
|
||||
|
||||
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
|
||||
MessageBox.Show("Button was clicked.")
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
```
|
||||
|
||||
### Configuring a window for MSBuild
|
||||
|
||||
How you implement your window determines how it's configured for MSBuild. For a window that is defined using both XAML markup and code-behind:
|
||||
|
||||
- XAML markup files are configured as MSBuild `Page` items.
|
||||
- Code-behind files are configured as MSBuild `Compile` items.
|
||||
|
||||
.NET SDK projects automatically import the correct `Page` and `Compile` items for you and you don't need to do declare these. When the project is configured for WPF, the XAML markup files are automatically imported as `Page` items, and the corresponding code-behind file is imported as `Compile`.
|
||||
|
||||
MSBuild projects won't automatically import the types and you must declare them yourself:
|
||||
|
||||
```xml
|
||||
<Project>
|
||||
...
|
||||
<Page Include="MarkupAndCodeBehindWindow.xaml" />
|
||||
<Compile Include=" MarkupAndCodeBehindWindow.xaml.cs" />
|
||||
...
|
||||
</Project>
|
||||
```
|
||||
|
||||
For information about building WPF applications, see [Compile a WPF Application](../../../framework/wpf/app-development/building-a-wpf-application-wpf.md).
|
||||
|
||||
## Window lifetime
|
||||
|
||||
As with any class, a window has a lifetime that begins when it's first instantiated, after which it's opened, activated/deactivated, and eventually closed.
|
||||
|
||||
### Opening a window
|
||||
|
||||
To open a window, you first create an instance of it, which is demonstrated in the following example:
|
||||
|
||||
:::code language="csharp" source="snippets/index_2/csharp/App.xaml.cs":::
|
||||
:::code language="vb" source="snippets/index_2/vb/Application.xaml.vb":::
|
||||
|
||||
In this example `Window1` is instantiated when the application starts, which occurs when the <xref:System.Windows.Application.Startup> event is raised. For more information about the startup window, see [How to get or set the main application window](how-to-get-set-main-application-window.md).
|
||||
|
||||
When a window is instantiated, a reference to it's automatically added to a [list of windows](xref:System.Windows.Application.Windows%2A) that is managed by the <xref:System.Windows.Application> object. The first window to be instantiated is automatically set by <xref:System.Windows.Application> as the [main application window](xref:System.Windows.Application.MainWindow%2A).
|
||||
|
||||
The window is finally opened by calling the <xref:System.Windows.Window.Show%2A> method as shown in the following image:
|
||||
|
||||
:::image type="content" source="./media/index/window-with-button.png" alt-text="WPF Window with a single button inside.":::
|
||||
|
||||
A window that is opened by calling <xref:System.Windows.Window.Show%2A> is a _modeless_ window, and the application doesn't prevent users from interacting with other windows in the application. Opening a window with <xref:System.Windows.Window.ShowDialog%2A> opens a window as _modal_ and restricts user interaction to the specific window. For more information, see [Dialog Boxes Overview](dialog-boxes-overview.md).
|
||||
|
||||
When <xref:System.Windows.Window.Show%2A> is called, a window does initialization work before it's shown to establish infrastructure that allows it to receive user input. When the window is initialized, the <xref:System.Windows.Window.SourceInitialized> event is raised and the window is shown.
|
||||
|
||||
For more information, see [How to open a window or dialog box](how-to-open-window-dialog-box.md).
|
||||
|
||||
#### Startup window
|
||||
|
||||
The previous example used the `Startup` event to run code that displayed the initial application window. As a shortcut, instead use <xref:System.Windows.Application.StartupUri%2A> to specify the path to a XAML file in your application. The application automatically creates and displays the window specified by that property.
|
||||
|
||||
:::code language="xaml" source="snippets/index/csharp/App.xaml" highlight="5":::
|
||||
|
||||
#### Window ownership
|
||||
|
||||
A window that is opened by using the <xref:System.Windows.Window.Show%2A> method doesn't have an implicit relationship with the window that created it. Users can interact with either window independently of the other, which means that either window can do the following:
|
||||
|
||||
- Cover the other (unless one of the windows has its <xref:System.Windows.Window.Topmost%2A> property set to `true`).
|
||||
- Be minimized, maximized, and restored without affecting the other.
|
||||
|
||||
Some windows require a relationship with the window that opens them. For example, an Integrated Development Environment (IDE) application may open property windows and tool windows whose typical behavior is to cover the window that creates them. Furthermore, such windows should always close, minimize, maximize, and restore in concert with the window that created them. Such a relationship can be established by making one window *own* another, and is achieved by setting the <xref:System.Windows.Window.Owner%2A> property of the *owned window* with a reference to the *owner window*. This is shown in the following example.
|
||||
|
||||
:::code language="csharp" source="snippets/index/csharp/ChildWindow1.xaml.cs" range="12-18":::
|
||||
:::code language="vb" source="snippets/index/vb/ChildWindow1.xaml.vb" range="2-7":::
|
||||
|
||||
After ownership is established:
|
||||
|
||||
- The owned window can reference its owner window by inspecting the value of its <xref:System.Windows.Window.Owner%2A> property.
|
||||
- The owner window can discover all the windows it owns by inspecting the value of its <xref:System.Windows.Window.OwnedWindows%2A> property.
|
||||
|
||||
### Window activation
|
||||
|
||||
When a window is first opened, it becomes the active window. The _active window_ is the window that is currently capturing user input, such as key strokes and mouse clicks. When a window becomes active, it raises the <xref:System.Windows.Window.Activated> event.
|
||||
|
||||
> [!NOTE]
|
||||
> When a window is first opened, the <xref:System.Windows.FrameworkElement.Loaded> and <xref:System.Windows.Window.ContentRendered> events are raised only after the <xref:System.Windows.Window.Activated> event is raised. With this in mind, a window can effectively be considered opened when <xref:System.Windows.Window.ContentRendered> is raised.
|
||||
|
||||
After a window becomes active, a user can activate another window in the same application, or activate another application. When that happens, the currently active window becomes deactivated and raises the <xref:System.Windows.Window.Deactivated> event. Likewise, when the user selects a currently deactivated window, the window becomes active again and <xref:System.Windows.Window.Activated> is raised.
|
||||
|
||||
One common reason to handle <xref:System.Windows.Window.Activated> and <xref:System.Windows.Window.Deactivated> is to enable and disable functionality that can only run when a window is active. For example, some windows display interactive content that requires constant user input or attention, including games and video players. The following example is a simplified video player that demonstrates how to handle <xref:System.Windows.Window.Activated> and <xref:System.Windows.Window.Deactivated> to implement this behavior.
|
||||
|
||||
:::code language="xaml" source="snippets/index/csharp/CustomMediaPlayerWindow.xaml":::
|
||||
|
||||
The following code is the code-behind for the XAML.
|
||||
|
||||
:::code language="csharp" source="snippets/index/csharp/CustomMediaPlayerWindow.xaml.cs":::
|
||||
:::code language="vb" source="snippets/index/vb/CustomMediaPlayerWindow.xaml.vb":::
|
||||
|
||||
Other types of applications may still run code in the background when a window is deactivated. For example, a mail client may continue polling the mail server while the user is using other applications. Applications like these often provide different or extra behavior while the main window is deactivated. For a mail program, this may mean both adding the new mail item to the inbox and adding a notification icon to the system tray. A notification icon need only be displayed when the mail window isn't active, which is determined by inspecting the <xref:System.Windows.Window.IsActive%2A> property.
|
||||
|
||||
If a background task completes, a window may want to notify the user more urgently by calling <xref:System.Windows.Window.Activate%2A> method. If the user is interacting with another application activated when <xref:System.Windows.Window.Activate%2A> is called, the window's taskbar button flashes. However, if a user is interacting with the current application, calling <xref:System.Windows.Window.Activate%2A> will bring the window to the foreground.
|
||||
|
||||
> [!NOTE]
|
||||
> You can handle application-scope activation using the <xref:System.Windows.Application.Activated?displayProperty=nameWithType> and <xref:System.Windows.Application.Deactivated?displayProperty=nameWithType> events.
|
||||
|
||||
#### Preventing window activation
|
||||
|
||||
There are scenarios where windows shouldn't be activated when shown, such as conversation windows of a chat application or notification windows of an email application.
|
||||
|
||||
If your application has a window that shouldn't be activated when shown, you can set its <xref:System.Windows.Window.ShowActivated%2A> property to `false` before calling the <xref:System.Windows.Window.Show%2A> method for the first time. As a consequence:
|
||||
|
||||
- The window isn't activated.
|
||||
- The window's <xref:System.Windows.Window.Activated> event isn't raised.
|
||||
- The currently activated window remains activated.
|
||||
|
||||
The window will become activated, however, as soon as the user activates it by clicking either the client or non-client area. In this case:
|
||||
|
||||
- The window is activated.
|
||||
- The window's <xref:System.Windows.Window.Activated> event is raised.
|
||||
- The previously activated window is deactivated.
|
||||
- The window's <xref:System.Windows.Window.Deactivated> and <xref:System.Windows.Window.Activated> events are then raised as expected in response to user actions.
|
||||
|
||||
### Closing a window
|
||||
|
||||
The life of a window starts coming to an end when a user closes it. Once a window is closed, it can't be reopened. A window can be closed by using elements in the non-client area, including the following:
|
||||
|
||||
- The **Close** item of the **System** menu.
|
||||
- Pressing <kbd>ALT + F4</kbd>.
|
||||
- Pressing the **Close** button.
|
||||
- Pressing <kbd>ESC</kbd> when a button has the <xref:System.Windows.Controls.Button.IsCancel%2A> property set to `true` on a modal window.
|
||||
|
||||
You can provide more mechanisms to the client area to close a window, the more common of which include the following:
|
||||
|
||||
- An **Exit** item in the **File** menu, typically for main application windows.
|
||||
- A **Close** item in the **File** menu, typically on a secondary application window.
|
||||
- A **Cancel** button, typically on a modal dialog box.
|
||||
- A **Close** button, typically on a modeless dialog box.
|
||||
|
||||
To close a window in response to one of these custom mechanisms, you need to call the <xref:System.Windows.Window.Close%2A> method. The following example implements the ability to close a window by choosing **Exit** from a **File** menu.
|
||||
|
||||
:::code language="xaml" source="snippets/index/csharp/ClosingWindow.xaml":::
|
||||
|
||||
The following code is the code-behind for the XAML.
|
||||
|
||||
:::code language="csharp" source="snippets/index/csharp/ClosingWindow.xaml.cs":::
|
||||
:::code language="vb" source="snippets/index/vb/ClosingWindow.xaml.vb":::
|
||||
|
||||
> [!NOTE]
|
||||
> An application can be configured to shut down automatically when either the main application window closes (see <xref:System.Windows.Application.MainWindow%2A>) or the last window closes. For more information, see <xref:System.Windows.Application.ShutdownMode%2A>.
|
||||
|
||||
While a window can be explicitly closed through mechanisms provided in the non-client and client areas, a window can also be implicitly closed as a result of behavior in other parts of the application or Windows, including the following:
|
||||
|
||||
- A user logs off or shuts down Windows.
|
||||
- A window's <xref:System.Windows.Window.Owner%2A> closes.
|
||||
- The main application window is closed and <xref:System.Windows.Application.ShutdownMode%2A> is <xref:System.Windows.ShutdownMode.OnMainWindowClose>.
|
||||
- <xref:System.Windows.Application.Shutdown%2A> is called.
|
||||
|
||||
> [!IMPORTANT]
|
||||
> A window can't be reopened after it's closed.
|
||||
|
||||
#### Cancel window closure
|
||||
|
||||
When a window closes, it raises two events: <xref:System.Windows.Window.Closing> and <xref:System.Windows.Window.Closed>.
|
||||
|
||||
<xref:System.Windows.Window.Closing> is raised before the window closes, and it provides a mechanism by which window closure can be prevented. One common reason to prevent window closure is if window content contains modified data. In this situation, the <xref:System.Windows.Window.Closing> event can be handled to determine whether data is dirty and, if so, to ask the user whether to either continue closing the window without saving the data or to cancel window closure. The following example shows the key aspects of handling <xref:System.Windows.Window.Closing>.
|
||||
|
||||
:::code language="xaml" source="snippets/index/csharp/DataWindow.xaml":::
|
||||
|
||||
The following code is the code-behind for the XAML.
|
||||
|
||||
:::code language="csharp" source="snippets/index/csharp/DataWindow.xaml.cs":::
|
||||
:::code language="vb" source="snippets/index/vb/DataWindow.xaml.vb":::
|
||||
|
||||
The <xref:System.Windows.Window.Closing> event handler is passed a <xref:System.ComponentModel.CancelEventArgs>, which implements the <xref:System.ComponentModel.CancelEventArgs.Cancel%2A> property that you set to `true` to prevent a window from closing.
|
||||
|
||||
If <xref:System.Windows.Window.Closing> isn't handled, or it's handled but not canceled, the window will close. Just before a window actually closes, <xref:System.Windows.Window.Closed> is raised. At this point, a window can't be prevented from closing.
|
||||
|
||||
### Window lifetime events
|
||||
|
||||
The following illustration shows the sequence of the principal events in the lifetime of a window:
|
||||
|
||||
:::image type="content" source="./media/index/window-lifetime-events.png" alt-text="Diagram that shows events in a window's lifetime.":::
|
||||
|
||||
The following illustration shows the sequence of the principal events in the lifetime of a window that is shown without activation (<xref:System.Windows.Window.ShowActivated%2A> is set to `false` before the window is shown):
|
||||
|
||||
:::image type="content" source="./media/index/window-lifetime-no-activation.png" alt-text="Diagram that shows events in a window's lifetime without activation.":::
|
||||
|
||||
## Window location
|
||||
|
||||
While a window is open, it has a location in the x and y dimensions relative to the desktop. This location can be determined by inspecting the <xref:System.Windows.Window.Left%2A> and <xref:System.Windows.Window.Top%2A> properties, respectively. Set these properties to change the location of the window.
|
||||
|
||||
You can also specify the initial location of a <xref:System.Windows.Window> when it first appears by setting the <xref:System.Windows.Window.WindowStartupLocation%2A> property with one of the following <xref:System.Windows.WindowStartupLocation> enumeration values:
|
||||
|
||||
- <xref:System.Windows.WindowStartupLocation.CenterOwner> (default)
|
||||
- <xref:System.Windows.WindowStartupLocation.CenterScreen>
|
||||
- <xref:System.Windows.WindowStartupLocation.Manual>
|
||||
|
||||
If the startup location is specified as <xref:System.Windows.WindowStartupLocation.Manual>, and the <xref:System.Windows.Window.Left%2A> and <xref:System.Windows.Window.Top%2A> properties have not been set, <xref:System.Windows.Window> will ask the operating system for a location to appear in.
|
||||
|
||||
### Topmost windows and z-order
|
||||
|
||||
Besides having an x and y location, a window also has a location in the z dimension, which determines its vertical position with respect to other windows. This is known as the window's z-order, and there are two types: **normal** z-order and **topmost** z-order. The location of a window in the *normal z-order* is determined by whether it's currently active or not. By default, a window is located in the normal z-order. The location of a window in the *topmost z-order* is also determined by whether it's currently active or not. Furthermore, windows in the topmost z-order are always located above windows in the normal z-order. A window is located in the topmost z-order by setting its <xref:System.Windows.Window.Topmost%2A> property to `true`.
|
||||
|
||||
Within each z-order type, the currently active window appears above all other windows in the same z-order.
|
||||
|
||||
## Window size
|
||||
|
||||
Besides having a desktop location, a window has a size that is determined by several properties, including the various width and height properties and <xref:System.Windows.Window.SizeToContent%2A>.
|
||||
|
||||
<xref:System.Windows.FrameworkElement.MinWidth%2A>, <xref:System.Windows.FrameworkElement.Width%2A>, and <xref:System.Windows.FrameworkElement.MaxWidth%2A> are used to manage the range of widths that a window can have during its lifetime.
|
||||
|
||||
```xaml
|
||||
<Window
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
MinWidth="300" Width="400" MaxWidth="500">
|
||||
</Window>
|
||||
```
|
||||
|
||||
Window height is managed by <xref:System.Windows.FrameworkElement.MinHeight%2A>, <xref:System.Windows.FrameworkElement.Height%2A>, and <xref:System.Windows.FrameworkElement.MaxHeight%2A>.
|
||||
|
||||
```xaml
|
||||
<Window
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
MinHeight="300" Height="400" MaxHeight="500">
|
||||
</Window>
|
||||
```
|
||||
|
||||
Because the various width values and height values each specify a range, it's possible for the width and height of a resizable window to be anywhere within the specified range for the respective dimension. To detect its current width and height, inspect <xref:System.Windows.FrameworkElement.ActualWidth%2A> and <xref:System.Windows.FrameworkElement.ActualHeight%2A>, respectively.
|
||||
|
||||
If you'd like the width and height of your window to have a size that fits to the size of the window's content, you can use the <xref:System.Windows.Window.SizeToContent%2A> property, which has the following values:
|
||||
|
||||
- [SizeToContent.Manual](xref:System.Windows.SizeToContent.Manual)\
|
||||
No effect (default).
|
||||
- [SizeToContent.Width](xref:System.Windows.SizeToContent.Width)\
|
||||
Fit to content width, which has the same effect as setting both <xref:System.Windows.FrameworkElement.MinWidth%2A> and <xref:System.Windows.FrameworkElement.MaxWidth%2A> to the width of the content.
|
||||
- [SizeToContent.Height](xref:System.Windows.SizeToContent.Height)\
|
||||
Fit to content height, which has the same effect as setting both <xref:System.Windows.FrameworkElement.MinHeight%2A> and <xref:System.Windows.FrameworkElement.MaxHeight%2A> to the height of the content.
|
||||
- [SizeToContent.WidthAndHeight](xref:System.Windows.SizeToContent.WidthAndHeight)\
|
||||
Fit to content width and height, which has the same effect as setting both <xref:System.Windows.FrameworkElement.MinHeight%2A> and <xref:System.Windows.FrameworkElement.MaxHeight%2A> to the height of the content, and setting both <xref:System.Windows.FrameworkElement.MinWidth%2A> and <xref:System.Windows.FrameworkElement.MaxWidth%2A> to the width of the content.
|
||||
|
||||
The following example shows a window that automatically sizes to fit its content, both vertically and horizontally, when first shown.
|
||||
|
||||
```xaml
|
||||
<Window
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
SizeToContent="WidthAndHeight">
|
||||
</Window>
|
||||
```
|
||||
|
||||
The following example shows how to set the <xref:System.Windows.Window.SizeToContent%2A> property in code to specify how a window resizes to fit its content .
|
||||
|
||||
```csharp
|
||||
// Manually alter window height and width
|
||||
this.SizeToContent = SizeToContent.Manual;
|
||||
|
||||
// Automatically resize width relative to content
|
||||
this.SizeToContent = SizeToContent.Width;
|
||||
|
||||
// Automatically resize height relative to content
|
||||
this.SizeToContent = SizeToContent.Height;
|
||||
|
||||
// Automatically resize height and width relative to content
|
||||
this.SizeToContent = SizeToContent.WidthAndHeight;
|
||||
```
|
||||
|
||||
```vb
|
||||
' Manually alter window height and width
|
||||
Me.SizeToContent = SizeToContent.Manual
|
||||
|
||||
' Automatically resize width relative to content
|
||||
Me.SizeToContent = SizeToContent.Width
|
||||
|
||||
' Automatically resize height relative to content
|
||||
Me.SizeToContent = SizeToContent.Height
|
||||
|
||||
' Automatically resize height and width relative to content
|
||||
Me.SizeToContent = SizeToContent.WidthAndHeight
|
||||
```
|
||||
|
||||
## Order of precedence for sizing properties
|
||||
|
||||
Essentially, the various sizes properties of a window combine to define the range of width and height for a resizable window. To ensure a valid range is maintained, <xref:System.Windows.Window> evaluates the values of the size properties using the following orders of precedence.
|
||||
|
||||
**For Height Properties:**
|
||||
|
||||
01. <xref:System.Windows.FrameworkElement.MinHeight%2A?displayProperty=nameWithType>
|
||||
01. <xref:System.Windows.FrameworkElement.MaxHeight%2A?displayProperty=nameWithType>
|
||||
01. <xref:System.Windows.SizeToContent.Height?displayProperty=nameWithType> / <xref:System.Windows.SizeToContent.WidthAndHeight?displayProperty=nameWithType>
|
||||
01. <xref:System.Windows.FrameworkElement.Height%2A?displayProperty=nameWithType>
|
||||
|
||||
**For Width Properties:**
|
||||
|
||||
01. <xref:System.Windows.FrameworkElement.MinWidth%2A?displayProperty=nameWithType>
|
||||
01. <xref:System.Windows.FrameworkElement.MaxWidth%2A?displayProperty=nameWithType>
|
||||
01. <xref:System.Windows.SizeToContent.Width?displayProperty=nameWithType> / <xref:System.Windows.SizeToContent.WidthAndHeight?displayProperty=nameWithType>
|
||||
01. <xref:System.Windows.FrameworkElement.Width%2A?displayProperty=nameWithType>
|
||||
|
||||
The order of precedence can also determine the size of a window when it's maximized, which is managed with the <xref:System.Windows.Window.WindowState%2A> property.
|
||||
|
||||
## Window state
|
||||
|
||||
During the lifetime of a resizable window, it can have three states: normal, minimized, and maximized. A window with a _normal_ state is the default state of a window. A window with this state allows a user to move and resize it by using a resize grip or the border, if it's resizable.
|
||||
|
||||
A window with a *minimized* state collapses to its task bar button if <xref:System.Windows.Window.ShowInTaskbar%2A> is set to `true`; otherwise, it collapses to the smallest possible size it can be and moves itself to the bottom-left corner of the desktop. Neither type of minimized window can be resized using a border or resize grip, although a minimized window that isn't shown in the task bar can be dragged around the desktop.
|
||||
|
||||
A window with a *maximized* state expands to the maximum size it can be, which will only be as large as its <xref:System.Windows.FrameworkElement.MaxWidth%2A>, <xref:System.Windows.FrameworkElement.MaxHeight%2A>, and <xref:System.Windows.Window.SizeToContent%2A> properties dictate. Like a minimized window, a maximized window can't be resized by using a resize grip or by dragging the border.
|
||||
|
||||
> [!NOTE]
|
||||
> The values of the <xref:System.Windows.Window.Top%2A>, <xref:System.Windows.Window.Left%2A>, <xref:System.Windows.FrameworkElement.Width%2A>, and <xref:System.Windows.FrameworkElement.Height%2A> properties of a window always represent the values for the normal state, even when the window is currently maximized or minimized.
|
||||
|
||||
The state of a window can be configured by setting its <xref:System.Windows.Window.WindowState%2A> property, which can have one of the following <xref:System.Windows.WindowState> enumeration values:
|
||||
|
||||
- <xref:System.Windows.WindowState.Normal> (default)
|
||||
- <xref:System.Windows.WindowState.Maximized>
|
||||
- <xref:System.Windows.WindowState.Minimized>
|
||||
|
||||
The following example shows how to create a window that is shown as maximized when it opens.
|
||||
|
||||
```xaml
|
||||
<Window
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
WindowState="Maximized">
|
||||
</Window>
|
||||
```
|
||||
|
||||
In general, you should set <xref:System.Windows.Window.WindowState%2A> to configure the initial state of a window. Once a _resizable_ window is shown, users can press the minimize, maximize, and restore buttons on the window's title bar to change the window state.
|
||||
|
||||
## Window appearance
|
||||
|
||||
You change the appearance of the client area of a window by adding window-specific content to it, such as buttons, labels, and text boxes. To configure the non-client area, <xref:System.Windows.Window> provides several properties, which include <xref:System.Windows.Window.Icon%2A> to set a window's icon and <xref:System.Windows.Window.Title%2A> to set its title.
|
||||
|
||||
You can also change the appearance and behavior of non-client area border by configuring a window's resize mode, window style, and whether it appears as a button in the desktop task bar.
|
||||
|
||||
### Resize mode
|
||||
|
||||
Depending on the <xref:System.Windows.Window.WindowStyle%2A> property, you can control if, and how, users resize the window. The window style affects the following:
|
||||
|
||||
- Allow or disallow resizing by dragging the window border with the mouse.
|
||||
- Whether the **Minimize**, **Maximize**, and **Close** buttons appear on the non-client area.
|
||||
- Whether the **Minimize**, **Maximize**, and **Close** buttons are enabled.
|
||||
|
||||
You can configure how a window resizes by setting its <xref:System.Windows.Window.ResizeMode%2A> property, which can be one of the following <xref:System.Windows.ResizeMode> enumeration values:
|
||||
|
||||
- <xref:System.Windows.ResizeMode.NoResize>
|
||||
- <xref:System.Windows.ResizeMode.CanMinimize>
|
||||
- <xref:System.Windows.ResizeMode.CanResize> (default)
|
||||
- <xref:System.Windows.ResizeMode.CanResizeWithGrip>
|
||||
|
||||
As with <xref:System.Windows.Window.WindowStyle%2A>, the resize mode of a window is unlikely to change during its lifetime, which means that you'll most likely set it from XAML markup.
|
||||
|
||||
```xaml
|
||||
<Window
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
ResizeMode="CanResizeWithGrip">
|
||||
</Window>
|
||||
```
|
||||
|
||||
Note that you can detect whether a window is maximized, minimized, or restored by inspecting the <xref:System.Windows.Window.WindowState%2A> property.
|
||||
|
||||
### Window style
|
||||
|
||||
The border that is exposed from the non-client area of a window is suitable for most applications. However, there are circumstances where different types of borders are needed, or no borders are needed at all, depending on the type of window.
|
||||
|
||||
To control what type of border a window gets, you set its <xref:System.Windows.Window.WindowStyle%2A> property with one of the following values of the <xref:System.Windows.WindowStyle> enumeration:
|
||||
|
||||
- <xref:System.Windows.WindowStyle.None>
|
||||
- <xref:System.Windows.WindowStyle.SingleBorderWindow> (default)
|
||||
- <xref:System.Windows.WindowStyle.ThreeDBorderWindow>
|
||||
- <xref:System.Windows.WindowStyle.ToolWindow>
|
||||
|
||||
The effect of applying a window style is illustrated in the following image:
|
||||
|
||||
:::image type="content" source="./media/index/window-styles.png" alt-text="Screenshot that shows how WindowStyle affects a window in WPF.":::
|
||||
|
||||
Notice that the image above doesn't show any noticeable difference between `SingleBorderWindow` and `ThreeDBorderWindow`. Back in Windows XP, `ThreeDBorderWindow` did affect how the window was drawn, adding a 3D border to the client area. Starting with Windows 7, the differences between the two styles are minimal.
|
||||
|
||||
You can set <xref:System.Windows.Window.WindowStyle%2A> using either XAML markup or code. Because it's unlikely to change during the lifetime of a window, you'll most likely configure it using XAML markup.
|
||||
|
||||
```xaml
|
||||
<Window
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
WindowStyle="ToolWindow">
|
||||
</Window>
|
||||
```
|
||||
|
||||
#### Non-rectangular window style
|
||||
|
||||
There are also situations where the border styles that <xref:System.Windows.Window.WindowStyle%2A> allows you to have aren't sufficient. For example, you may want to create an application with a non-rectangular border, like Microsoft Windows Media Player uses.
|
||||
|
||||
For example, consider the speech bubble window shown in the following image:
|
||||
|
||||
:::image type="content" source="./media/index/window-transparent.png" alt-text="Screenshot of a WPF window that has a clipped area and custom shape.":::
|
||||
|
||||
This type of window can be created by setting the <xref:System.Windows.Window.WindowStyle%2A> property to <xref:System.Windows.WindowStyle.None>, and by using special support that <xref:System.Windows.Window> has for transparency.
|
||||
|
||||
:::code language="xaml" source="snippets/index/csharp/ClippedWindow.xaml":::
|
||||
|
||||
This combination of values instructs the window to render transparent. In this state, the window's non-client area adornment buttons can't be used and you need to provide your own.
|
||||
|
||||
### Task bar presence
|
||||
|
||||
The default appearance of a window includes a taskbar button. Some types of windows don't have a task bar button, such as message boxes, [dialog boxes](dialog-boxes-overview.md), or windows with the <xref:System.Windows.Window.WindowStyle%2A> property set to <xref:System.Windows.WindowStyle.ToolWindow>. You can control whether the task bar button for a window is shown by setting the <xref:System.Windows.Window.ShowInTaskbar%2A> property, which is `true` by default.
|
||||
|
||||
```xaml
|
||||
<Window
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
ShowInTaskbar="False">
|
||||
</Window>
|
||||
```
|
||||
|
||||
## Other types of windows
|
||||
|
||||
<xref:System.Windows.Navigation.NavigationWindow> is a window that is designed to host navigable content.<!-- For more information, see [Navigation Overview](navigation-overview.md)).-->
|
||||
|
||||
Dialog boxes are windows that are often used to gather information from a user to complete a function. For example, when a user wants to open a file, the **Open File** dialog box is displayed by an application to get the file name from the user. For more information, see [Dialog Boxes Overview](dialog-boxes-overview.md).
|
||||
|
||||
## See also
|
||||
|
||||
- [Dialog boxes overview](dialog-boxes-overview.md)
|
||||
- [How to open a window or dialog box](how-to-open-window-dialog-box.md)
|
||||
- [How to open a common dialog box](how-to-open-common-system-dialog-box.md)
|
||||
- [How to open a message box](how-to-open-message-box.md)
|
||||
- [How to close a window or dialog box](how-to-close-window-dialog-box.md)
|
||||
- <xref:System.Windows.Window?displayProperty=fullName>
|
||||
- <xref:System.Windows.MessageBox?displayProperty=fullName>
|
||||
- <xref:System.Windows.Navigation.NavigationWindow?displayProperty=fullName>
|
||||
- <xref:System.Windows.Application?displayProperty=fullName>
|
||||
|
After Width: | Height: | Size: 6.8 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 9.0 KiB |
|
After Width: | Height: | Size: 8.7 KiB |
|
After Width: | Height: | Size: 19 KiB |
|
After Width: | Height: | Size: 28 KiB |
|
After Width: | Height: | Size: 25 KiB |
|
After Width: | Height: | Size: 29 KiB |
|
After Width: | Height: | Size: 1.7 KiB |
|
After Width: | Height: | Size: 3.1 KiB |
|
After Width: | Height: | Size: 9.3 KiB |
|
After Width: | Height: | Size: 4.6 KiB |
|
After Width: | Height: | Size: 3.6 KiB |
|
After Width: | Height: | Size: 14 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 16 KiB |
|
After Width: | Height: | Size: 10 KiB |
|
After Width: | Height: | Size: 9.6 KiB |
|
After Width: | Height: | Size: 11 KiB |
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 7.3 KiB |
|
After Width: | Height: | Size: 12 KiB |
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="Dialogs.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:Dialogs"
|
||||
StartupUri="Window1.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace Dialogs
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
private void Application_Startup(object sender, StartupEventArgs e)
|
||||
{
|
||||
// Create the window
|
||||
Window1 window = new Window1();
|
||||
|
||||
// Open the window
|
||||
window.Show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace Dialogs
|
||||
{
|
||||
public class MarginValidationRule : ValidationRule
|
||||
{
|
||||
public double MinMargin { get; set; }
|
||||
public double MaxMargin { get; set; }
|
||||
|
||||
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
||||
{
|
||||
double margin;
|
||||
|
||||
// Is a number?
|
||||
if (!double.TryParse((string)value, out margin))
|
||||
{
|
||||
return new ValidationResult(false, "Not a number.");
|
||||
}
|
||||
|
||||
// Is in range?
|
||||
if ((margin < MinMargin) || (margin > MaxMargin))
|
||||
{
|
||||
var msg = $"Margin must be between {MinMargin} and {MaxMargin}.";
|
||||
return new ValidationResult(false, msg);
|
||||
}
|
||||
|
||||
// Number is valid
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<Window x:Class="Dialogs.Margins"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
Title="Change Margins"
|
||||
Closing="Window_Closing"
|
||||
MinHeight="200"
|
||||
MinWidth="300"
|
||||
SizeToContent="WidthAndHeight"
|
||||
ResizeMode="NoResize"
|
||||
ShowInTaskbar="False"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
FocusManager.FocusedElement="{Binding ElementName=leftMarginTextBox}">
|
||||
<Grid Margin="10">
|
||||
<Grid.Resources>
|
||||
<!-- Default settings for controls -->
|
||||
<Style TargetType="{x:Type Label}">
|
||||
<Setter Property="Margin" Value="0,3,5,5" />
|
||||
<Setter Property="Padding" Value="0,0,0,5" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type TextBox}">
|
||||
<Setter Property="Margin" Value="0,0,0,5" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type Button}">
|
||||
<Setter Property="Width" Value="70" />
|
||||
<Setter Property="Height" Value="25" />
|
||||
<Setter Property="Margin" Value="5,0,0,0" />
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Left,Top,Right,Bottom margins-->
|
||||
<Label Grid.Column="0" Grid.Row="0">Left Margin:</Label>
|
||||
<TextBox Name="leftMarginTextBox" Grid.Column="1" Grid.Row="0" />
|
||||
|
||||
<Label Grid.Column="0" Grid.Row="1">Top Margin:</Label>
|
||||
<TextBox Name="topMarginTextBox" Grid.Column="1" Grid.Row="1"/>
|
||||
|
||||
<Label Grid.Column="0" Grid.Row="2">Right Margin:</Label>
|
||||
<TextBox Name="rightMarginTextBox" Grid.Column="1" Grid.Row="2" />
|
||||
|
||||
<Label Grid.Column="0" Grid.Row="3">Bottom Margin:</Label>
|
||||
<TextBox Name="bottomMarginTextBox" Grid.Column="1" Grid.Row="3" />
|
||||
|
||||
<!-- Accept or Cancel -->
|
||||
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4" Orientation="Horizontal" HorizontalAlignment="Right">
|
||||
<Button Name="okButton" Click="okButton_Click" IsDefault="True">OK</Button>
|
||||
<Button Name="cancelButton" IsCancel="True">Cancel</Button>
|
||||
</StackPanel>
|
||||
</Grid >
|
||||
</Window>
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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;
|
||||
|
||||
namespace Dialogs
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for Margins.xaml
|
||||
/// </summary>
|
||||
public partial class Margins : Window
|
||||
{
|
||||
public Margins()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void okButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DialogResult = true;
|
||||
}
|
||||
|
||||
// <CancelAndHide>
|
||||
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
{
|
||||
// First, cancel the window closure
|
||||
e.Cancel = true;
|
||||
|
||||
// Next, hide the window
|
||||
Hide();
|
||||
}
|
||||
// </CancelAndHide>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<Window x:Class="Dialogs.Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Dialogs"
|
||||
Width="500" Height="165">
|
||||
|
||||
<StackPanel>
|
||||
<Button Click="Button_Click">Message Box</Button>
|
||||
<Button Click="Save_Click">Save file</Button>
|
||||
<Button Click="Open_Click">Open file</Button>
|
||||
<Button Click="Print_Click">Print</Button>
|
||||
<Button Click="formatMarginsButton_Click" Content="_Margins..."/>
|
||||
</StackPanel>
|
||||
|
||||
</Window>
|
||||
@@ -0,0 +1,81 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace Dialogs
|
||||
{
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Save_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Open_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void Print_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
private void formatMarginsButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
Method2();
|
||||
|
||||
}
|
||||
|
||||
// <DialogResultButtons>
|
||||
private void okButton_Click(object sender, RoutedEventArgs e) =>
|
||||
DialogResult = true;
|
||||
|
||||
private void cancelButton_Click(object sender, RoutedEventArgs e) =>
|
||||
DialogResult = false;
|
||||
// </DialogResultButtons>
|
||||
|
||||
private void Method()
|
||||
{
|
||||
// <HandleClosing>
|
||||
var marginsWindow = new Margins();
|
||||
|
||||
marginsWindow.Closed += (sender, eventArgs) =>
|
||||
{
|
||||
MessageBox.Show($"You closed the margins window! It had the title of {marginsWindow.Title}");
|
||||
};
|
||||
|
||||
marginsWindow.Show();
|
||||
// </HandleClosing>
|
||||
}
|
||||
|
||||
private void Method2()
|
||||
{
|
||||
// <HandleDialogResponse>
|
||||
var dialog = new Margins();
|
||||
|
||||
// Display the dialog box and read the response
|
||||
bool? result = dialog.ShowDialog();
|
||||
|
||||
if (result == true)
|
||||
{
|
||||
// User accepted the dialog box
|
||||
MessageBox.Show("Your request will be processed.");
|
||||
}
|
||||
else
|
||||
{
|
||||
// User cancelled the dialog box
|
||||
MessageBox.Show("Sorry it didn't work out, we'll try again later.");
|
||||
}
|
||||
// </HandleDialogResponse>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
|
||||
<security>
|
||||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<!-- UAC Manifest Options
|
||||
If you want to change the Windows User Account Control level replace the
|
||||
requestedExecutionLevel node with one of the following.
|
||||
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
|
||||
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
|
||||
|
||||
Specifying requestedExecutionLevel element will disable file and registry virtualization.
|
||||
Remove this element if your application requires this virtualization for backwards
|
||||
compatibility.
|
||||
-->
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- A list of the Windows versions that this application has been tested on
|
||||
and is designed to work with. Uncomment the appropriate elements
|
||||
and Windows will automatically select the most compatible environment. -->
|
||||
|
||||
<!-- Windows Vista -->
|
||||
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
|
||||
|
||||
<!-- Windows 7 -->
|
||||
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
|
||||
|
||||
<!-- Windows 8 -->
|
||||
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
|
||||
|
||||
<!-- Windows 8.1 -->
|
||||
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
|
||||
|
||||
<!-- Windows 10 -->
|
||||
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
|
||||
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
|
||||
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
|
||||
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
|
||||
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
|
||||
<!--
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
-->
|
||||
|
||||
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
|
||||
<dependency>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity
|
||||
type="win32"
|
||||
name="Microsoft.Windows.Common-Controls"
|
||||
version="6.0.0.0"
|
||||
processorArchitecture="*"
|
||||
publicKeyToken="6595b64144ccf1df"
|
||||
language="*"
|
||||
/>
|
||||
</dependentAssembly>
|
||||
</dependency>
|
||||
|
||||
</assembly>
|
||||
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="Application"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:Dialogs"
|
||||
StartupUri="Window1.xaml">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -0,0 +1,6 @@
|
||||
Class Application
|
||||
|
||||
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
|
||||
' can be handled in this file.
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,11 @@
|
||||
Imports System.Windows
|
||||
|
||||
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
|
||||
'1st parameter: where theme specific resource dictionaries are located
|
||||
'(used if a resource is not found in the page,
|
||||
' or application resource dictionaries)
|
||||
|
||||
'2nd parameter: where the generic resource dictionary is located
|
||||
'(used if a resource is not found in the page,
|
||||
'app, and any theme specific resource dictionaries)
|
||||
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<RootNamespace>Dialogs</RootNamespace>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Import Include="System.Windows" />
|
||||
<Import Include="System.Windows.Controls" />
|
||||
<Import Include="System.Windows.Data" />
|
||||
<Import Include="System.Windows.Documents" />
|
||||
<Import Include="System.Windows.Input" />
|
||||
<Import Include="System.Windows.Media" />
|
||||
<Import Include="System.Windows.Media.Imaging" />
|
||||
<Import Include="System.Windows.Navigation" />
|
||||
<Import Include="System.Windows.Shapes" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,26 @@
|
||||
Imports System.Globalization
|
||||
|
||||
Public Class MarginValidationRule
|
||||
Inherits ValidationRule
|
||||
|
||||
Public Property MinMargin As Double
|
||||
Public Property MaxMargin As Double
|
||||
|
||||
Public Overrides Function Validate(value As Object, cultureInfo As CultureInfo) As ValidationResult
|
||||
Dim margin As Double
|
||||
|
||||
' Is it a number?
|
||||
If Not Double.TryParse(DirectCast(value, String), margin) Then
|
||||
Return New ValidationResult(False, "Not a number")
|
||||
End If
|
||||
|
||||
' Is it in range
|
||||
If margin < MinMargin Or margin > MaxMargin Then
|
||||
Return New ValidationResult(False, $"Margin must be between {MinMargin} and {MaxMargin}")
|
||||
End If
|
||||
|
||||
Return New ValidationResult(True, Nothing)
|
||||
|
||||
End Function
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,115 @@
|
||||
<Window x:Class="Margins"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Dialogs"
|
||||
Title="Margins"
|
||||
Closing="Window_Closing"
|
||||
MinHeight="200"
|
||||
MinWidth="300"
|
||||
SizeToContent="WidthAndHeight"
|
||||
ResizeMode="CanResizeWithGrip"
|
||||
ShowInTaskbar="False"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
WindowStyle="ToolWindow"
|
||||
FocusManager.FocusedElement="{Binding ElementName=leftMarginTextBox}">
|
||||
<Grid>
|
||||
|
||||
<Grid.Resources>
|
||||
<Style TargetType="{x:Type Grid}">
|
||||
<Setter Property="Margin" Value="10" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type Label}">
|
||||
<Setter Property="Margin" Value="0,3,5,5" />
|
||||
<Setter Property="Padding" Value="0,0,0,5" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type TextBox}">
|
||||
<Setter Property="Margin" Value="0,0,0,5" />
|
||||
<Style.Triggers>
|
||||
<Trigger Property="Validation.HasError" Value="true">
|
||||
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
<Style TargetType="{x:Type StackPanel}">
|
||||
<Setter Property="Orientation" Value="Horizontal" />
|
||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type Button}">
|
||||
<Setter Property="Width" Value="70" />
|
||||
<Setter Property="Height" Value="25" />
|
||||
<Setter Property="Margin" Value="5,0,0,0" />
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Left Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="0">Left Margin:</Label>
|
||||
<TextBox Name="leftMarginTextBox" Grid.Column="1" Grid.Row="0">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Left" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Top Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="1">Top Margin:</Label>
|
||||
<TextBox Name="topMarginTextBox" Grid.Column="1" Grid.Row="1">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Top" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Right Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="2">Right Margin:</Label>
|
||||
<TextBox Name="rightMarginTextBox" Grid.Column="1" Grid.Row="2">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Right" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Bottom Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="3">Bottom Margin:</Label>
|
||||
<TextBox Name="bottomMarginTextBox" Grid.Column="1" Grid.Row="3">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Bottom" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Accept or Cancel -->
|
||||
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4">
|
||||
<Button Name="okButton" IsDefault="True" Click="okButton_Click">OK</Button>
|
||||
<Button Name="cancelButton" IsCancel="True">Cancel</Button>
|
||||
</StackPanel>
|
||||
|
||||
</Grid >
|
||||
</Window>
|
||||
@@ -0,0 +1,15 @@
|
||||
Public Class Margins
|
||||
Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
|
||||
End Sub
|
||||
|
||||
'<CancelAndHide>
|
||||
Private Sub Window_Closing(sender As Object, e As ComponentModel.CancelEventArgs)
|
||||
'First, cancel the window closure
|
||||
e.Cancel = True
|
||||
|
||||
'Next, hide the window
|
||||
Hide()
|
||||
End Sub
|
||||
'</CancelAndHide>
|
||||
End Class
|
||||
@@ -0,0 +1,16 @@
|
||||
<Window x:Class="Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:Dialogs"
|
||||
mc:Ignorable="d"
|
||||
Title="Names" Height="180" Width="260">
|
||||
<StackPanel>
|
||||
<Button>Message Box</Button>
|
||||
<Button>Save file</Button>
|
||||
<Button>Open file</Button>
|
||||
<Button>Print</Button>
|
||||
<Button Click="formatMarginsButton_Click" Content="_Margins..."/>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
@@ -0,0 +1,45 @@
|
||||
Public Class Window1
|
||||
'<DialogResultButtons>
|
||||
Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
DialogResult = True
|
||||
End Sub
|
||||
|
||||
Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
DialogResult = False
|
||||
End Sub
|
||||
'</DialogResultButtons>
|
||||
|
||||
Private Sub Method1()
|
||||
'<HandleClosing>
|
||||
Dim marginsWindow As New Margins
|
||||
|
||||
AddHandler marginsWindow.Closed, Sub(sender As Object, e As EventArgs)
|
||||
MessageBox.Show($"You closed the margins window! It had the title of {marginsWindow.Title}")
|
||||
End Sub
|
||||
|
||||
marginsWindow.Show()
|
||||
'</HandleClosing>
|
||||
End Sub
|
||||
|
||||
Private Sub formatMarginsButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
Method1()
|
||||
End Sub
|
||||
|
||||
Private Sub Method2()
|
||||
'<HandleDialogResponse>
|
||||
Dim marginsWindow As New Margins
|
||||
|
||||
Dim result As Boolean? = marginsWindow.ShowDialog()
|
||||
|
||||
If result = True Then
|
||||
' User accepted the dialog box
|
||||
MessageBox.Show("Your request will be processed.")
|
||||
Else
|
||||
' User cancelled the dialog box
|
||||
MessageBox.Show("Sorry it didn't work out, we'll try again later.")
|
||||
End If
|
||||
|
||||
marginsWindow.Show()
|
||||
'</HandleDialogResponse>
|
||||
End Sub
|
||||
End Class
|
||||
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="CloseWindows.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
Startup="Application_Startup">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -0,0 +1,16 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace CloseWindows
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
private void Application_Startup(object sender, StartupEventArgs e)
|
||||
{
|
||||
// Create the window
|
||||
Window1 window = new Window1();
|
||||
|
||||
// Open the window
|
||||
window.Show();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
<ApplicationManifest>app.manifest</ApplicationManifest>
|
||||
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace CloseWindows
|
||||
{
|
||||
public class MarginValidationRule : ValidationRule
|
||||
{
|
||||
public double MinMargin { get; set; }
|
||||
public double MaxMargin { get; set; }
|
||||
|
||||
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
|
||||
{
|
||||
double margin;
|
||||
|
||||
// Is a number?
|
||||
if (!double.TryParse((string)value, out margin))
|
||||
{
|
||||
return new ValidationResult(false, "Not a number.");
|
||||
}
|
||||
|
||||
// Is in range?
|
||||
if ((margin < MinMargin) || (margin > MaxMargin))
|
||||
{
|
||||
var msg = $"Margin must be between {MinMargin} and {MaxMargin}.";
|
||||
return new ValidationResult(false, msg);
|
||||
}
|
||||
|
||||
// Number is valid
|
||||
return new ValidationResult(true, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
<Window x:Class="CloseWindows.Margins"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
Title="Margins"
|
||||
MinHeight="200"
|
||||
MinWidth="300"
|
||||
SizeToContent="WidthAndHeight"
|
||||
ResizeMode="CanResizeWithGrip"
|
||||
ShowInTaskbar="False"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
WindowStyle="ToolWindow"
|
||||
Closing="Window_Closing"
|
||||
FocusManager.FocusedElement="{Binding ElementName=leftMarginTextBox}">
|
||||
<Grid>
|
||||
|
||||
<Grid.Resources>
|
||||
<Style TargetType="{x:Type Grid}">
|
||||
<Setter Property="Margin" Value="10" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type Label}">
|
||||
<Setter Property="Margin" Value="0,3,5,5" />
|
||||
<Setter Property="Padding" Value="0,0,0,5" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type TextBox}">
|
||||
<Setter Property="Margin" Value="0,0,0,5" />
|
||||
<Style.Triggers>
|
||||
<Trigger Property="Validation.HasError" Value="true">
|
||||
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
<Style TargetType="{x:Type StackPanel}">
|
||||
<Setter Property="Orientation" Value="Horizontal" />
|
||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type Button}">
|
||||
<Setter Property="Width" Value="70" />
|
||||
<Setter Property="Height" Value="25" />
|
||||
<Setter Property="Margin" Value="5,0,0,0" />
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Left Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="0">Left Margin:</Label>
|
||||
<TextBox Name="leftMarginTextBox" Grid.Column="1" Grid.Row="0">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Left" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Top Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="1">Top Margin:</Label>
|
||||
<TextBox Name="topMarginTextBox" Grid.Column="1" Grid.Row="1">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Top" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Right Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="2">Right Margin:</Label>
|
||||
<TextBox Name="rightMarginTextBox" Grid.Column="1" Grid.Row="2">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Right" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Bottom Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="3">Bottom Margin:</Label>
|
||||
<TextBox Name="bottomMarginTextBox" Grid.Column="1" Grid.Row="3">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Bottom" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Accept or Cancel -->
|
||||
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4">
|
||||
<Button Name="okButton" IsDefault="True" Click="okButton_Click">OK</Button>
|
||||
<Button Name="cancelButton" IsCancel="True">Cancel</Button>
|
||||
</StackPanel>
|
||||
|
||||
</Grid >
|
||||
</Window>
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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;
|
||||
|
||||
namespace CloseWindows
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for Margins.xaml
|
||||
/// </summary>
|
||||
public partial class Margins : Window
|
||||
{
|
||||
public Margins()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void okButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
DialogResult = true;
|
||||
}
|
||||
|
||||
// <CancelAndHide>
|
||||
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
||||
{
|
||||
// Cancel the closure
|
||||
e.Cancel = true;
|
||||
|
||||
// Hide the window
|
||||
Hide();
|
||||
}
|
||||
// </CancelAndHide>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<Window x:Class="CloseWindows.Menus"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
mc:Ignorable="d"
|
||||
Title="Simple Text Editor"
|
||||
Width="400" Height="230">
|
||||
<Grid>
|
||||
<Grid.Resources>
|
||||
<Style TargetType="{x:Type Button}">
|
||||
<Setter Property="Width" Value="70" />
|
||||
<Setter Property="Height" Value="25" />
|
||||
<Setter Property="Margin" Value="5,0,0,0" />
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Menu>
|
||||
<MenuItem Header="File">
|
||||
<MenuItem Header="Open..." />
|
||||
<Separator />
|
||||
<MenuItem Header="Save" />
|
||||
<MenuItem Header="Save As..." />
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
<TextBox Grid.Row="1" AcceptsReturn="True" TextWrapping="Wrap" Margin="5">
|
||||
<TextBox.Text>This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. </TextBox.Text>
|
||||
</TextBox>
|
||||
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
|
||||
<Button Name="closeButton" Click="closeButton_Click">Close</Button>
|
||||
<Button Name="saveButton" Click="saveButton_Click">Save</Button>
|
||||
<Button Name="saveAsButton">Save As...</Button>
|
||||
<Button x:Name="okButton" IsDefault="True" Content="OK" Click="okButton_Click"/>
|
||||
<!--<CancelButton>-->
|
||||
<Button x:Name="cancelButton" Content="Cancel" IsCancel="true"/>
|
||||
<!--</CancelButton>-->
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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;
|
||||
|
||||
namespace CloseWindows
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for Menus.xaml
|
||||
/// </summary>
|
||||
public partial class Menus : Window
|
||||
{
|
||||
public Menus()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
// <CloseDialog>
|
||||
private void okButton_Click(object sender, RoutedEventArgs e) =>
|
||||
DialogResult = true;
|
||||
|
||||
private void cancelButton_Click(object sender, RoutedEventArgs e) =>
|
||||
DialogResult = false;
|
||||
// </CloseDialog>
|
||||
|
||||
// <CloseNormal>
|
||||
private void closeButton_Click(object sender, RoutedEventArgs e) =>
|
||||
Close();
|
||||
// </CloseNormal>
|
||||
|
||||
// <Hide>
|
||||
private void saveButton_Click(object sender, RoutedEventArgs e) =>
|
||||
Hide();
|
||||
// </Hide>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
<Window x:Class="CloseWindows.Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
Title="Window Example"
|
||||
Width="300" Height="165">
|
||||
|
||||
<StackPanel>
|
||||
<Button Click="formatMarginsButton_Click" Content="_Margins..."/>
|
||||
<Button Click="showMenus_Click" Content="Menus" />
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Button Name="okButton" Click="okButton_Click">OK</Button>
|
||||
<!-- <CancelButton> -->
|
||||
<Button Name="cancelButton" IsCancel="True">Cancel</Button>
|
||||
<!-- </CancelButton> -->
|
||||
</StackPanel>
|
||||
</StackPanel>
|
||||
|
||||
</Window>
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace CloseWindows
|
||||
{
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void formatMarginsButton_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
// <ShowDialog>
|
||||
var window = new Margins();
|
||||
window.Owner = this;
|
||||
window.ShowDialog();
|
||||
// </ShowDialog>
|
||||
}
|
||||
|
||||
private void showMenus_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
var menu = new Menus();
|
||||
var result = menu.ShowDialog();
|
||||
menu.Show();
|
||||
}
|
||||
|
||||
// <DialogResultButtons>
|
||||
private void okButton_Click(object sender, RoutedEventArgs e) =>
|
||||
DialogResult = true;
|
||||
|
||||
private void cancelButton_Click(object sender, RoutedEventArgs e) =>
|
||||
DialogResult = false;
|
||||
// </DialogResultButtons>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
|
||||
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
|
||||
<security>
|
||||
<requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<!-- UAC Manifest Options
|
||||
If you want to change the Windows User Account Control level replace the
|
||||
requestedExecutionLevel node with one of the following.
|
||||
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
|
||||
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
|
||||
<requestedExecutionLevel level="highestAvailable" uiAccess="false" />
|
||||
|
||||
Specifying requestedExecutionLevel element will disable file and registry virtualization.
|
||||
Remove this element if your application requires this virtualization for backwards
|
||||
compatibility.
|
||||
-->
|
||||
<requestedExecutionLevel level="asInvoker" uiAccess="false" />
|
||||
</requestedPrivileges>
|
||||
</security>
|
||||
</trustInfo>
|
||||
|
||||
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
|
||||
<application>
|
||||
<!-- A list of the Windows versions that this application has been tested on
|
||||
and is designed to work with. Uncomment the appropriate elements
|
||||
and Windows will automatically select the most compatible environment. -->
|
||||
|
||||
<!-- Windows Vista -->
|
||||
<!--<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}" />-->
|
||||
|
||||
<!-- Windows 7 -->
|
||||
<!--<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}" />-->
|
||||
|
||||
<!-- Windows 8 -->
|
||||
<!--<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}" />-->
|
||||
|
||||
<!-- Windows 8.1 -->
|
||||
<!--<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}" />-->
|
||||
|
||||
<!-- Windows 10 -->
|
||||
<!--<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}" />-->
|
||||
|
||||
</application>
|
||||
</compatibility>
|
||||
|
||||
<!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher
|
||||
DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need
|
||||
to opt in. Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should
|
||||
also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
|
||||
<!--
|
||||
<application xmlns="urn:schemas-microsoft-com:asm.v3">
|
||||
<windowsSettings>
|
||||
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
|
||||
</windowsSettings>
|
||||
</application>
|
||||
-->
|
||||
|
||||
<!-- Enable themes for Windows common controls and dialogs (Windows XP and later) -->
|
||||
<dependency>
|
||||
<dependentAssembly>
|
||||
<assemblyIdentity
|
||||
type="win32"
|
||||
name="Microsoft.Windows.Common-Controls"
|
||||
version="6.0.0.0"
|
||||
processorArchitecture="*"
|
||||
publicKeyToken="6595b64144ccf1df"
|
||||
language="*"
|
||||
/>
|
||||
</dependentAssembly>
|
||||
</dependency>
|
||||
|
||||
</assembly>
|
||||
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="Application"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
Startup="Application_Startup">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -0,0 +1,11 @@
|
||||
Class Application
|
||||
|
||||
Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
|
||||
' Create the window
|
||||
Dim window As New Window1
|
||||
|
||||
' Open the window
|
||||
window.Show()
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,11 @@
|
||||
Imports System.Windows
|
||||
|
||||
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
|
||||
'1st parameter: where theme specific resource dictionaries are located
|
||||
'(used if a resource is not found in the page,
|
||||
' or application resource dictionaries)
|
||||
|
||||
'2nd parameter: where the generic resource dictionary is located
|
||||
'(used if a resource is not found in the page,
|
||||
'app, and any theme specific resource dictionaries)
|
||||
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
|
||||
@@ -0,0 +1,3 @@
|
||||
Public Class Class1
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<RootNamespace>CloseWindows</RootNamespace>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Import Include="System.Windows" />
|
||||
<Import Include="System.Windows.Controls" />
|
||||
<Import Include="System.Windows.Data" />
|
||||
<Import Include="System.Windows.Documents" />
|
||||
<Import Include="System.Windows.Input" />
|
||||
<Import Include="System.Windows.Media" />
|
||||
<Import Include="System.Windows.Media.Imaging" />
|
||||
<Import Include="System.Windows.Navigation" />
|
||||
<Import Include="System.Windows.Shapes" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,26 @@
|
||||
Imports System.Globalization
|
||||
|
||||
Public Class MarginValidationRule
|
||||
Inherits ValidationRule
|
||||
|
||||
Public Property MinMargin As Double
|
||||
Public Property MaxMargin As Double
|
||||
|
||||
Public Overrides Function Validate(value As Object, cultureInfo As CultureInfo) As ValidationResult
|
||||
Dim margin As Double
|
||||
|
||||
' Is it a number?
|
||||
If Not Double.TryParse(DirectCast(value, String), margin) Then
|
||||
Return New ValidationResult(False, "Not a number")
|
||||
End If
|
||||
|
||||
' Is it in range
|
||||
If margin < MinMargin Or margin > MaxMargin Then
|
||||
Return New ValidationResult(False, $"Margin must be between {MinMargin} and {MaxMargin}")
|
||||
End If
|
||||
|
||||
Return New ValidationResult(True, Nothing)
|
||||
|
||||
End Function
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,115 @@
|
||||
<Window x:Class="Margins"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
Title="Margins"
|
||||
MinHeight="200"
|
||||
MinWidth="300"
|
||||
SizeToContent="WidthAndHeight"
|
||||
ResizeMode="CanResizeWithGrip"
|
||||
ShowInTaskbar="False"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
WindowStyle="ToolWindow"
|
||||
Closing="Window_Closing"
|
||||
FocusManager.FocusedElement="{Binding ElementName=leftMarginTextBox}">
|
||||
<Grid>
|
||||
|
||||
<Grid.Resources>
|
||||
<Style TargetType="{x:Type Grid}">
|
||||
<Setter Property="Margin" Value="10" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type Label}">
|
||||
<Setter Property="Margin" Value="0,3,5,5" />
|
||||
<Setter Property="Padding" Value="0,0,0,5" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type TextBox}">
|
||||
<Setter Property="Margin" Value="0,0,0,5" />
|
||||
<Style.Triggers>
|
||||
<Trigger Property="Validation.HasError" Value="true">
|
||||
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
|
||||
</Trigger>
|
||||
</Style.Triggers>
|
||||
</Style>
|
||||
<Style TargetType="{x:Type StackPanel}">
|
||||
<Setter Property="Orientation" Value="Horizontal" />
|
||||
<Setter Property="HorizontalAlignment" Value="Right" />
|
||||
</Style>
|
||||
<Style TargetType="{x:Type Button}">
|
||||
<Setter Property="Width" Value="70" />
|
||||
<Setter Property="Height" Value="25" />
|
||||
<Setter Property="Margin" Value="5,0,0,0" />
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="Auto" />
|
||||
<ColumnDefinition />
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- Left Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="0">Left Margin:</Label>
|
||||
<TextBox Name="leftMarginTextBox" Grid.Column="1" Grid.Row="0">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Left" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Top Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="1">Top Margin:</Label>
|
||||
<TextBox Name="topMarginTextBox" Grid.Column="1" Grid.Row="1">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Top" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Right Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="2">Right Margin:</Label>
|
||||
<TextBox Name="rightMarginTextBox" Grid.Column="1" Grid.Row="2">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Right" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Bottom Margin -->
|
||||
<Label Grid.Column="0" Grid.Row="3">Bottom Margin:</Label>
|
||||
<TextBox Name="bottomMarginTextBox" Grid.Column="1" Grid.Row="3">
|
||||
<TextBox.Text>
|
||||
<Binding Path="Bottom" UpdateSourceTrigger="PropertyChanged">
|
||||
<Binding.ValidationRules>
|
||||
<local:MarginValidationRule MinMargin="0" MaxMargin="10" />
|
||||
</Binding.ValidationRules>
|
||||
</Binding>
|
||||
</TextBox.Text>
|
||||
</TextBox>
|
||||
|
||||
<!-- Accept or Cancel -->
|
||||
<StackPanel Grid.Column="0" Grid.ColumnSpan="2" Grid.Row="4">
|
||||
<Button Name="okButton" IsDefault="True" Click="okButton_Click">OK</Button>
|
||||
<Button Name="cancelButton" IsCancel="True">Cancel</Button>
|
||||
</StackPanel>
|
||||
|
||||
</Grid >
|
||||
</Window>
|
||||
@@ -0,0 +1,15 @@
|
||||
Public Class Margins
|
||||
Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
|
||||
End Sub
|
||||
|
||||
'<CancelAndHide>
|
||||
Private Sub Window_Closing(sender As Object, e As ComponentModel.CancelEventArgs)
|
||||
' Cancel the closure
|
||||
e.Cancel = True
|
||||
|
||||
' Hide the window
|
||||
Hide()
|
||||
End Sub
|
||||
'</CancelAndHide>
|
||||
End Class
|
||||
@@ -0,0 +1,44 @@
|
||||
<Window x:Class="Menus"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
mc:Ignorable="d"
|
||||
Title="Simple Text Editor"
|
||||
Width="400" Height="230">
|
||||
<Grid>
|
||||
<Grid.Resources>
|
||||
<Style TargetType="{x:Type Button}">
|
||||
<Setter Property="Width" Value="70" />
|
||||
<Setter Property="Height" Value="25" />
|
||||
<Setter Property="Margin" Value="5,0,0,0" />
|
||||
</Style>
|
||||
</Grid.Resources>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto" />
|
||||
<RowDefinition Height="*" />
|
||||
<RowDefinition Height="Auto" />
|
||||
</Grid.RowDefinitions>
|
||||
<Menu>
|
||||
<MenuItem Header="File">
|
||||
<MenuItem Header="Open..." />
|
||||
<Separator />
|
||||
<MenuItem Header="Save" />
|
||||
<MenuItem Header="Save As..." />
|
||||
</MenuItem>
|
||||
</Menu>
|
||||
<TextBox Grid.Row="1" AcceptsReturn="True" TextWrapping="Wrap" Margin="5">
|
||||
<TextBox.Text>This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor. This is a text editor.</TextBox.Text>
|
||||
</TextBox>
|
||||
<StackPanel Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Right" Margin="5">
|
||||
<Button Name="closeButton" Click="closeButton_Click">Close</Button>
|
||||
<Button Name="saveButton" Click="saveButton_Click">Save</Button>
|
||||
<Button Name="saveAsButton">Save As...</Button>
|
||||
<Button x:Name="okButton" IsDefault="True" Content="OK" Click="okButton_Click"/>
|
||||
<!--<CancelButton>-->
|
||||
<Button x:Name="cancelButton" Content="Cancel" IsCancel="true"/>
|
||||
<!--</CancelButton>-->
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,23 @@
|
||||
Public Class Menus
|
||||
'<CloseDialog>
|
||||
Private Sub okButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
DialogResult = True
|
||||
End Sub
|
||||
|
||||
Private Sub cancelButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
DialogResult = False
|
||||
End Sub
|
||||
'</CloseDialog>
|
||||
|
||||
'<CloseNormal>
|
||||
Private Sub closeButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
Close()
|
||||
End Sub
|
||||
'</CloseNormal>
|
||||
|
||||
'<Hide>
|
||||
Private Sub saveButton_Click(sender As Object, e As RoutedEventArgs)
|
||||
Hide()
|
||||
End Sub
|
||||
'</Hide>
|
||||
End Class
|
||||
@@ -0,0 +1,15 @@
|
||||
<Window x:Class="Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:CloseWindows"
|
||||
mc:Ignorable="d"
|
||||
Title="Names" Height="180" Width="260">
|
||||
<StackPanel>
|
||||
<Button Click="Button_Click">Message Box</Button>
|
||||
<Button Click="Save_Click">Save file</Button>
|
||||
<Button Click="Open_Click">Open file</Button>
|
||||
<Button Click="Print_Click">Print</Button>
|
||||
</StackPanel>
|
||||
</Window>
|
||||
@@ -0,0 +1,79 @@
|
||||
Public Class Window1
|
||||
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
|
||||
'<YesNoCancel>
|
||||
Dim messageBoxText As String = "Do you want to save changes?"
|
||||
Dim caption As String = "Word Processor"
|
||||
Dim Button As MessageBoxButton = MessageBoxButton.YesNoCancel
|
||||
Dim Icon As MessageBoxImage = MessageBoxImage.Warning
|
||||
Dim result As MessageBoxResult
|
||||
|
||||
'<MessageBoxShow>
|
||||
result = MessageBox.Show(messageBoxText, caption, Button, Icon)
|
||||
'</YesNoCancel>
|
||||
|
||||
Select Case result
|
||||
Case MessageBoxResult.Cancel
|
||||
' User pressed Cancel
|
||||
Case MessageBoxResult.Yes
|
||||
' User pressed Yes
|
||||
Case MessageBoxResult.No
|
||||
' User pressed No
|
||||
End Select
|
||||
'</MessageBoxShow>
|
||||
End Sub
|
||||
|
||||
Private Sub Save_Click(sender As Object, e As RoutedEventArgs)
|
||||
'<SaveFile>
|
||||
' Configure save file dialog box
|
||||
Dim dialog As New Microsoft.Win32.SaveFileDialog()
|
||||
dialog.FileName = "Document" ' Default file name
|
||||
dialog.DefaultExt = ".txt" ' Default file extension
|
||||
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension
|
||||
|
||||
' Show save file dialog box
|
||||
Dim result As Boolean? = dialog.ShowDialog()
|
||||
|
||||
' Process save file dialog box results
|
||||
If result = True Then
|
||||
' Save document
|
||||
Dim filename As String = dialog.FileName
|
||||
End If
|
||||
'</SaveFile>
|
||||
End Sub
|
||||
|
||||
Private Sub Open_Click(sender As Object, e As RoutedEventArgs)
|
||||
'<OpenFile>
|
||||
' Configure open file dialog box
|
||||
Dim dialog As New Microsoft.Win32.OpenFileDialog()
|
||||
dialog.FileName = "Document" ' Default file name
|
||||
dialog.DefaultExt = ".txt" ' Default file extension
|
||||
dialog.Filter = "Text documents (.txt)|*.txt" ' Filter files by extension
|
||||
|
||||
' Show open file dialog box
|
||||
Dim result As Boolean? = dialog.ShowDialog()
|
||||
|
||||
' Process open file dialog box results
|
||||
If result = True Then
|
||||
' Open document
|
||||
Dim filename As String = dialog.FileName
|
||||
End If
|
||||
'</OpenFile>
|
||||
End Sub
|
||||
|
||||
Private Sub Print_Click(sender As Object, e As RoutedEventArgs)
|
||||
'<Print>
|
||||
' Configure printer dialog box
|
||||
Dim dialog As New System.Windows.Controls.PrintDialog()
|
||||
dialog.PageRangeSelection = System.Windows.Controls.PageRangeSelection.AllPages
|
||||
dialog.UserPageRangeEnabled = True
|
||||
|
||||
' Show save file dialog box
|
||||
Dim result As Boolean? = dialog.ShowDialog()
|
||||
|
||||
' Process save file dialog box results
|
||||
If result = True Then
|
||||
' Print document
|
||||
End If
|
||||
'</Print>
|
||||
End Sub
|
||||
End Class
|
||||
@@ -0,0 +1,7 @@
|
||||
<Application x:Class="MainApp.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:MainApp"
|
||||
StartupUri="Window1.xaml">
|
||||
|
||||
</Application>
|
||||
@@ -0,0 +1,22 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace MainApp
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
private void Application_Startup(object sender, StartupEventArgs e)
|
||||
{
|
||||
// <MainWindowDirect>
|
||||
Application.Current.MainWindow = new Window2();
|
||||
|
||||
Application.Current.MainWindow.Show();
|
||||
// </MainWindowDirect>
|
||||
|
||||
// <MainWindowIndirect>
|
||||
var appWindow = new Window2();
|
||||
|
||||
appWindow.Show();
|
||||
// </MainWindowIndirect>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="MainApp.Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:MainApp"
|
||||
Width="500" Height="165">
|
||||
|
||||
<!-- Client area containing the content of the window -->
|
||||
<Button Click="Button_Click">Click This Button</Button>
|
||||
|
||||
</Window>
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace MainApp
|
||||
{
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
// <GetMainWindow>
|
||||
private void Button_Click(object sender, RoutedEventArgs e) =>
|
||||
MessageBox.Show($"The main window's title is: {Application.Current.MainWindow.Title}");
|
||||
// </GetMainWindow>
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="MainApp.Window2"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:MainApp"
|
||||
mc:Ignorable="d"
|
||||
Title="Window2" Height="450" Width="800">
|
||||
<Grid>
|
||||
<TextBlock Text="Hi! I'm the second window!" />
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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;
|
||||
|
||||
namespace MainApp
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for Window2.xaml
|
||||
/// </summary>
|
||||
public partial class Window2 : Window
|
||||
{
|
||||
public Window2()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="Application"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-MainApppace:MainApp"
|
||||
Startup="Application_Startup">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
@@ -0,0 +1,17 @@
|
||||
Class Application
|
||||
|
||||
Private Sub Application_Startup(sender As Object, e As StartupEventArgs)
|
||||
'<MainWindowDirect>
|
||||
Application.Current.MainWindow = New Window2()
|
||||
|
||||
Application.Current.MainWindow.Show()
|
||||
'</MainWindowDirect>
|
||||
|
||||
'<MainWindowIndirect>
|
||||
Dim appWindow As New Window2()
|
||||
|
||||
appWindow.Show()
|
||||
'</MainWindowIndirect>
|
||||
End Sub
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,11 @@
|
||||
Imports System.Windows
|
||||
|
||||
'The ThemeInfo attribute describes where any theme specific and generic resource dictionaries can be found.
|
||||
'1st parameter: where theme specific resource dictionaries are located
|
||||
'(used if a resource is not found in the page,
|
||||
' or application resource dictionaries)
|
||||
|
||||
'2nd parameter: where the generic resource dictionary is located
|
||||
'(used if a resource is not found in the page,
|
||||
'app, and any theme specific resource dictionaries)
|
||||
<Assembly: ThemeInfo(ResourceDictionaryLocation.None, ResourceDictionaryLocation.SourceAssembly)>
|
||||
@@ -0,0 +1,3 @@
|
||||
Public Class Class1
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,22 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<RootNamespace>WindowsOverview</RootNamespace>
|
||||
<UseWPF>true</UseWPF>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Import Include="System.Windows" />
|
||||
<Import Include="System.Windows.Controls" />
|
||||
<Import Include="System.Windows.Data" />
|
||||
<Import Include="System.Windows.Documents" />
|
||||
<Import Include="System.Windows.Input" />
|
||||
<Import Include="System.Windows.Media" />
|
||||
<Import Include="System.Windows.Media.Imaging" />
|
||||
<Import Include="System.Windows.Navigation" />
|
||||
<Import Include="System.Windows.Shapes" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,11 @@
|
||||
<Window x:Class="Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:WindowsOverview"
|
||||
mc:Ignorable="d"
|
||||
Title="Names" Height="180" Width="260">
|
||||
<!-- Client area containing the content of the window -->
|
||||
<Button Click="Button_Click">Click This Button</Button>
|
||||
</Window>
|
||||
@@ -0,0 +1,7 @@
|
||||
Public Class Window1
|
||||
'<GetMainWindow>
|
||||
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
|
||||
MessageBox.Show($"The main window's title is: {Application.Current.MainWindow.Title}")
|
||||
End Sub
|
||||
'</GetMainWindow>
|
||||
End Class
|
||||
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="Window2"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:WindowsOverview"
|
||||
mc:Ignorable="d"
|
||||
Title="Window2" Height="450" Width="800">
|
||||
<Grid>
|
||||
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,3 @@
|
||||
Public Class Window2
|
||||
|
||||
End Class
|
||||
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="MainApp.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:MainApp">
|
||||
|
||||
<Application.MainWindow>
|
||||
<local:Window2 Visibility="Visible" />
|
||||
</Application.MainWindow>
|
||||
</Application>
|
||||
@@ -0,0 +1,12 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace MainApp
|
||||
{
|
||||
public partial class App : Application
|
||||
{
|
||||
private void Application_Startup(object sender, StartupEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
using System.Windows;
|
||||
|
||||
[assembly: ThemeInfo(
|
||||
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
|
||||
//(used if a resource is not found in the page,
|
||||
// or application resource dictionaries)
|
||||
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
|
||||
//(used if a resource is not found in the page,
|
||||
// app, or any theme specific resource dictionaries)
|
||||
)]
|
||||
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>WinExe</OutputType>
|
||||
<TargetFramework>net5.0-windows</TargetFramework>
|
||||
<UseWPF>true</UseWPF>
|
||||
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="MainApp.Window1"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:MainApp"
|
||||
Width="500" Height="165">
|
||||
|
||||
<!-- Client area containing the content of the window -->
|
||||
<Button Click="Button_Click">Click This Button</Button>
|
||||
|
||||
</Window>
|
||||
@@ -0,0 +1,17 @@
|
||||
using System.Windows;
|
||||
|
||||
namespace MainApp
|
||||
{
|
||||
public partial class Window1 : Window
|
||||
{
|
||||
public Window1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
private void Button_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<Window x:Class="MainApp.Window2"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:MainApp"
|
||||
mc:Ignorable="d"
|
||||
Title="Window2" Height="450" Width="800">
|
||||
<Grid>
|
||||
<TextBlock Text="Hi! I'm the second window!" />
|
||||
</Grid>
|
||||
</Window>
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
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;
|
||||
|
||||
namespace MainApp
|
||||
{
|
||||
/// <summary>
|
||||
/// Interaction logic for Window2.xaml
|
||||
/// </summary>
|
||||
public partial class Window2 : Window
|
||||
{
|
||||
public Window2()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Application x:Class="WindowsOverview.App"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:WindowsOverview"
|
||||
Startup="Application_Startup">
|
||||
<Application.Resources>
|
||||
|
||||
</Application.Resources>
|
||||
</Application>
|
||||