LoginSignup
6
7

More than 5 years have passed since last update.

WPF画面とやり取りするための最低限の構成

Last updated at Posted at 2019-02-05

概要

WPF画面とやり取りするための最低限の機能を備えた構成をつくる。

要点

やりたいこと

・画面(MainWindow.xaml)のコントロールに、ビューモデル(ViewModel.cs)のプロパティをバインドし、そのプロパティの変更を通知できる。
・画面(MainWindow.xaml)のコントロールに、ICommand(を継承したクラス=DelegateCommand)を使ってコマンド登録できる。

つまり、データの画面表示ができて、画面操作したときになにかアクションさせることができる最低限の構成のイメージ。

変化点

キャプチャ.JPG

BindingBase.cs
using System.ComponentModel;

namespace WpfApp1
{
    class BindingBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// プロパティが変更されたことを通知する.
        /// </summary>
        /// <param name="propertyName">プロパティ名</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}
DelegateCommand.cs
using System.Windows.Input;

namespace WpfApp1
{
    public class DelegateCommand : ICommand
    {
        System.Action execute;
        System.Func<bool> canExecute;

        public bool CanExecute(object parameter)
        {
            return canExecute();
        }

        public event System.EventHandler CanExecuteChanged
        {
            add { CommandManager.RequerySuggested += value; }
            remove { CommandManager.RequerySuggested -= value; }
        }

        public void Execute(object parameter)
        {
            execute();
        }

        public DelegateCommand(System.Action execute)
        {
            this.execute = execute;
        }

        public DelegateCommand(System.Action execute, System.Func<bool> canExecute)
        {
            this.execute = execute;
            this.canExecute = canExecute;
        }
    }
}
ViewModel.cs
namespace WpfApp1
{
    class ViewModel : BindingBase
    {
        public string VmMyText
        {
            get { return _vmMyText; }
            set { _vmMyText = value; OnPropertyChanged(nameof(VmMyText)); }
        }
        public string _vmMyText = "あいうえお";

        public DelegateCommand VmMyCommand { get; private set; }

        public ViewModel()
        {
            // コマンドの中身 登録
            VmMyCommand = new DelegateCommand(
                () =>
                {
                    VmMyText = "12345";
                },
                () =>
                {
                    // ボタンが押せるかどうかを決める処理(trueが「押せる」)
                    return true;
                });
        }
    }
}
MainView.xaml
<Window x:Class="WpfApp1.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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <!--  Viewで直接指定  -->
        <Button
            Content="{Binding VmMyText}"
            Command="{Binding VmMyCommand}"            
            />
    </Grid>
</Window>

image.png

コード

備考

勉強中のため、上記で使っている単語とその意味に誤りあるかもしれません。

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7