Window

De Banane Atomic
Aller à la navigationAller à la recherche

Propriétés utiles

Propriété Valeur
WindowStyle ToolWindow
ResizeMode NoResize
SizeToContent WidthAndHeight
WindowStartupLocation CenterOwner
Title Mon Titre

Design properties

Xaml.svg
<Window x:Class="MyNamespace.MainWindow"
        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:MyNamespace"
        mc:Ignorable="d"
        Title="MainWindow"
        Height="450"
        Width="800"
        d:DataContext="{d:DesignInstance local:MainWindowViewModel}">

Événement Closed vs Closing

Closing est appelé juste après la méthode Window.Close().
La gestion de l’événement Closing permet d’annuler la fermeture de la fenêtre.

Csharp.svg
void Window_Closing(object sender, CancelEventArgs e)
{
    e.Cancel = true;

Closed est lui appelé juste avant que le fenêtre ne se ferme.

Maximiser une fenêtre

Xaml.svg
<Window WindowState="Maximized">
Csharp.svg
Application.Current.MainWindow.WindowState = WindowState.Maximized;

Héritage de fenêtre

Ici Window1 hérite de WindowBase, cela n'est possible que si WindowsBase ne contient pas de XAML.

Csharp.svg
namespace Vues
{
    public class WindowBase : Window { ... }
    public class Window1 : WindowBase { ... }
}
Xaml.svg
<vues:WindowBase x:Class="Vues.Windows1"
                 xmlns:vues="clr-namespace:Vues">

Ouvrir une seconde fenêtre

Csharp.svg
// depuis le code behind
var childWindow = new Window() { Owner = this };  // définir la fenêtre courante comme parente
childWindow.Height = 100; childWindow.Width = 50;
var stackPanel = new StackPanel();

var button = new Button();
button.Content = "Ok";
// le bouton de validation est celui qui possède la propriété IsDefault à true
button.IsDefault = true;
button.Click += (_sender, _e) => { window.DialogResult = true; window.Close(); };
stackPanel.Children.Add(button);

childWindow.Content = stackPanel;
// définit la position de la fenêtre
childWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
childWindow.ShowDialog();

Renvoit la window contenant le UserControl

Csharp.svg
// renvoit la window contenant le UserControl
Window.GetWindow(monUserControl);

Padding

Ajouter une border transparente fait office de padding.

Xaml.svg
<Window>
    <Border BorderThickness="4" BorderBrush="Transparent">