Last active
December 2, 2017 08:10
-
-
Save yoshiiz/283aff4dfe151b0e9d0fadc90b4f8217 to your computer and use it in GitHub Desktop.
[Visual C# 2017][WPF] WpfCloseWindowCommandSample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Input; | |
namespace WpfCloseWindowCommandSample.Commands | |
{ | |
public class DelegateCommand<T> : ICommand | |
{ | |
private Action<T> _execute; | |
private Func<T, bool> _canExecute; | |
public DelegateCommand(Action<T> execute, Func<T, bool> canExecute) | |
{ | |
_execute = execute; | |
_canExecute = canExecute; | |
} | |
public void Execute(object parameter) | |
{ | |
_execute((T)parameter); | |
} | |
public bool CanExecute(object parameter) | |
{ | |
return _canExecute((T)parameter); | |
} | |
public event EventHandler CanExecuteChanged | |
{ | |
add { CommandManager.RequerySuggested += value; } | |
remove { CommandManager.RequerySuggested -= value; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Window x:Class="WpfCloseWindowCommandSample.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:WpfCloseWindowCommandSample" | |
mc:Ignorable="d" | |
xmlns:vm="clr-namespace:WpfCloseWindowCommandSample.ViewModels" | |
Title="MainWindow" Height="350" Width="525"> | |
<Window.DataContext> | |
<vm:MainViewModel /> | |
</Window.DataContext> | |
<Grid> | |
<Grid.ColumnDefinitions> | |
<ColumnDefinition /> | |
</Grid.ColumnDefinitions> | |
<Grid.RowDefinitions> | |
<RowDefinition Height="Auto" /> | |
<RowDefinition /> | |
</Grid.RowDefinitions> | |
<Menu Grid.Row="0" Grid.Column="0"> | |
<MenuItem Header="ファイル(_F)"> | |
<MenuItem Header="ウィンドウを閉じる" | |
Command="{Binding Path=CloseWindowCommand}" | |
CommandParameter="{Binding RelativeSource={ | |
RelativeSource Mode=FindAncestor, | |
AncestorType={x:Type Window}}}" /> | |
</MenuItem> | |
</Menu> | |
<TextBox Grid.Row="1" Grid.Column="0" /> | |
</Grid> | |
</Window> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
namespace WpfCloseWindowCommandSample | |
{ | |
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Windows; | |
using System.ComponentModel; // INotifyPropertyChanged | |
using WpfCloseWindowCommandSample.Commands; | |
namespace WpfCloseWindowCommandSample.ViewModels | |
{ | |
public class MainViewModel : INotifyPropertyChanged | |
{ | |
// --------------------------------------------------------------------- | |
// プロパティ - コマンド | |
private DelegateCommand<Window> _closeWindowCommand; | |
public DelegateCommand<Window> CloseWindowCommand | |
{ | |
get | |
{ | |
if (_closeWindowCommand == null) | |
{ | |
_closeWindowCommand = new DelegateCommand<Window>( | |
(window) => this.DoCloseWindow(window), | |
(window) => true | |
); | |
} | |
return _closeWindowCommand; | |
} | |
} | |
private void DoCloseWindow(Window window) | |
{ | |
window.Close(); | |
} | |
// --------------------------------------------------------------------- | |
// コンストラクタ | |
public MainViewModel() { } | |
// --------------------------------------------------------------------- | |
// インターフェースの実装 - INotifyPropertyChanged | |
public event PropertyChangedEventHandler PropertyChanged; | |
private void OnPropertyChanged(string name) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment