« Window » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
 
Aucun résumé des modifications
Ligne 1 : Ligne 1 :
[[Category:WPF]]
[[Category:WPF]]
= Événement Closed vs Closing =
[http://msdn.microsoft.com/en-us/library/system.windows.window.closing%28v=vs.110%29.aspx Closing] est appelé juste après la méthode <tt>Window.Close()</tt>.<br>
La gestion de l’événement Closing permet d’annuler la fermeture de la fenêtre.
<kode lang=csharp>
void Window_Closing(object sender, CancelEventArgs e)
{
    e.Cancel = true;
</kode>
[http://msdn.microsoft.com/en-us/library/system.windows.window.closed%28v=vs.110%29.aspx Closed] est lui appelé juste avant que le fenêtre ne se ferme.
= Propriétés utiles =
= Propriétés utiles =
{| class="wikitable wtp"  
{| class="wikitable wtp"  
Ligne 26 : Ligne 15 :
| Title || Mon Titre
| Title || Mon Titre
|}
|}
= Design properties =
<kode lang='xaml'>
<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}">
</kode>
= Événement Closed vs Closing =
[http://msdn.microsoft.com/en-us/library/system.windows.window.closing%28v=vs.110%29.aspx Closing] est appelé juste après la méthode <tt>Window.Close()</tt>.<br>
La gestion de l’événement Closing permet d’annuler la fermeture de la fenêtre.
<kode lang=csharp>
void Window_Closing(object sender, CancelEventArgs e)
{
    e.Cancel = true;
</kode>
[http://msdn.microsoft.com/en-us/library/system.windows.window.closed%28v=vs.110%29.aspx Closed] est lui appelé juste avant que le fenêtre ne se ferme.


= Maximiser une fenêtre =
= Maximiser une fenêtre =

Version du 13 mai 2020 à 13:56

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">