« Window » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
Aucun résumé des modifications |
|||
(5 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 16 : | Ligne 16 : | ||
! Valeur | ! Valeur | ||
|- | |- | ||
| WindowStyle || ToolWindow | | [https://docs.microsoft.com/fr-fr/dotnet/api/system.windows.window.windowstyle WindowStyle] || {{boxx|None}} {{boxx|ToolWindow}} {{boxx|SingleBorderWindow}} (default) {{boxx|ThreeDBorderWindow}} | ||
|- | |- | ||
| ResizeMode || NoResize | | ResizeMode || NoResize | ||
Ligne 68 : | Ligne 68 : | ||
</kode> | </kode> | ||
= | = Open a child window = | ||
<filebox fn='MainWindow.xaml'> | |||
<Button Command="{Binding OpenCommand}" /> | |||
</filebox> | |||
<filebox fn='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(); | |||
} | |||
</filebox> | |||
<filebox fn='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> | |||
</filebox> | |||
<filebox fn='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; | |||
} | |||
</filebox> | |||
== Code-behind == | |||
<kode lang=csharp> | <kode lang=csharp> | ||
// depuis le code behind | // depuis le code behind |
Dernière version du 13 mai 2020 à 14:39
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"> |