Window
Apparence
XML Namespaces
<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">
|
Useful properties
Propriété | Valeur |
---|---|
WindowStyle | None ToolWindow SingleBorderWindow (default) ThreeDBorderWindow |
ResizeMode | NoResize |
SizeToContent | WidthAndHeight |
WindowStartupLocation | CenterOwner |
Title | Mon Titre |
Design properties
<Window d:Height="450"
d: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.
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
<Window WindowState="Maximized">
|
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.
namespace Vues
{
public class WindowBase : Window { ... }
public class Window1 : WindowBase { ... }
}
|
<vues:WindowBase x:Class="Vues.Windows1"
xmlns:vues="clr-namespace:Vues">
|
Open a child window
MainWindow.xaml |
<Button Command="{Binding OpenCommand}" />
|
MainWindowViewModel.cs |
public ICommand OpenCommand { get; set; }
public MainWindowViewModel()
{
OpenCommand = new DelegateCommand(this.Open);
}
private void Open()
{
var childWindow = new ChildWindow();
var childWindowReturnedValue = childWindow.ShowDialog();
}
|
ChildWindow.xaml |
<Window x:Class="WpfApp1.ChildWindow"
x:Name="Window">
<DockPanel HorizontalAlignment="Center">
<StackPanel Orientation="Horizontal"
DockPanel.Dock="Bottom">
<!-- IsDefault="True" makes the focus on the button, so press enter makes the button click -->
<Button Content="Ok"
IsDefault="True"
Command="{Binding OkCommand}"
CommandParameter="{Binding ElementName=Window} "/>
<Button Content="Cancel"
IsCancel="True" /> <!-- IsCancel="True" makes the button close the window when clicked and return false -->
</StackPanel>
<TextBlock>
Some text ...
</TextBlock>
|
ChildWindowViewModel.cs |
public ICommand OkCommand { get; set; }
public ChildWindowViewModel()
{
OkCommand = new DelegateCommand<Window>(this.Ok);
}
private void Ok(Window window)
{
// set a value to DialogResult to close the window
window.DialogResult = true;
}
|
Code-behind
// 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
// renvoit la window contenant le UserControl
Window.GetWindow(monUserControl);
|
Padding
Ajouter une border transparente fait office de padding.
<Window>
<Border BorderThickness="4" BorderBrush="Transparent">
|