全て
| カテゴリ未分類
| 通常
| 数学
| 投資
| 副業
| 転職・就職
| ブログ
| 株Stock
| VBA
| SharePoint
| REST API
| RenderListDataAsStream
| PowerAutomateDesktop
| Graph API
| PowerApps
| PowerAutomate
| PowerFx
| OfficeScriptオフィススクリプト
| PowerAutomate日時Excel日時相互関係
| PowerShell
| Dataverse
| UiPath
| OneDrive
| LINQ
| VB.NET
| C#.NET
| WPFC#.NET
| Git
カテゴリ:WPFC#.NET
WPF (Windows Presentation Foundation) で `Command` クラスを利用することで、MVVM (Model-View-ViewModel) パターンを実現しやすくなります。ここでは、`ICommand` インターフェースを実装したカスタムコマンドクラスを作成し、それをボタンにバインドするサンプルコードを紹介します。 ### 1. コマンドクラスの作成 まず、`ICommand` インターフェースを実装したカスタムコマンドクラスを作成します。 ```csharp using System; using System.Windows.Input; namespace WpfApp { public class RelayCommand : ICommand { private readonly Action<object> execute; private readonly Predicate<object> canExecute; public RelayCommand(Action<object> execute, Predicate<object> canExecute = null) { this.execute = execute ?? throw new ArgumentNullException(nameof(execute)); this.canExecute = canExecute; } public bool CanExecute(object parameter) { return canExecute == null || canExecute(parameter); } public void Execute(object parameter) { execute(parameter); } public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } } } ``` ### 2. ViewModelクラスの作成 次に、コマンドを利用するViewModelクラスを作成します。 ```csharp using System.Windows; using System.Windows.Input; namespace WpfApp { public class MainViewModel { public ICommand ShowMessageCommand { get; } public MainViewModel() { ShowMessageCommand = new RelayCommand(ShowMessage); } private void ShowMessage(object parameter) { MessageBox.Show("Hello, WPF Command!"); } } } ``` ### 3. XAMLファイルの設定 次に、XAMLファイルでViewModelをデータコンテキストに設定し、ボタンにコマンドをバインドします。 ```xml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="200" Width="300"> <Window.DataContext> <local:MainViewModel /> </Window.DataContext> <Grid> <Button Content="Click Me" Command="{Binding ShowMessageCommand}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="50"/> </Grid> </Window> ``` ### 4. MainWindow.xaml.csの設定 次に、XAMLで使用するために、ViewModelの名前空間を指定します。 ```csharp using System.Windows; namespace WpfApp { public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } } } ``` ```xml <Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp" Title="MainWindow" Height="200" Width="300"> <Window.DataContext> <local:MainViewModel /> </Window.DataContext> <Grid> <Button Content="Click Me" Command="{Binding ShowMessageCommand}" HorizontalAlignment="Center" VerticalAlignment="Center" Width="100" Height="50"/> </Grid> </Window> ``` ### 5. アプリケーションの実行 以上の設定を行い、アプリケーションを実行すると、ボタンをクリックしたときにメッセージボックスが表示されます。 ### 補足説明 - `RelayCommand` クラスは、`ICommand` インターフェースを実装しており、コマンドのロジックと実行条件を外部から渡すことができます。 - `MainViewModel` クラスでは、`RelayCommand` を使用してコマンドを定義しています。 - XAMLファイルでボタンにコマンドをバインドすることで、UIとロジックの分離を実現しています。 この方法を使うことで、WPFアプリケーションでMVVMパターンを効果的に実装できます。 お気に入りの記事を「いいね!」で応援しよう
Last updated
2024.06.09 10:15:46
[WPFC#.NET] カテゴリの最新記事
|