SHOEISHA iD

※旧SEメンバーシップ会員の方は、同じ登録情報(メールアドレス&パスワード)でログインいただけます

DeveloperZine(デベロッパージン)- エンジニアの意思決定を支える技術情報メディア ProductZine

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

特集記事

WPFアプリケーションで腕試し
~C++でもWPFアプリを

Visual Studio ぜんぶのせ!

ビューとロジックの橋渡し部分の作成

 お次はMainWindow(ビュー)とCounterModel(ロジック)との仲介役となるMainWindowViewModelをVBで実装します。MainWindowsViewModelはMainWindowからバインドされるため、インターフェース:INotifyPropertyChangedを実装(Implements)しなければなりません。カウント値(Countプロパティ)の変更はイベント:NotifyPropertyChangedによってビューに通知されます。また、MainWindowの+/-ボタンにバインドされたIncCommand/DecCommandについてはそのままCounterModelに横流しするための「中継ぎ」になっています。

MainWindowViewModel.vb
Imports System.ComponentModel
Imports System.Windows.Input

Namespace DataBindingSample

  Public Class MainWindowViewModel
    Implements INotifyPropertyChanged

    Public Property Count() As Integer
      Get
        Return count_
      End Get
      Set(ByVal value As Integer)
        count_ = value
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("Count"))
      End Set
    End Property

    Public Event PropertyChanged As PropertyChangedEventHandler _
      Implements INotifyPropertyChanged.PropertyChanged

    Public Sub Update(ByVal c As Integer)
      Count = c
    End Sub

    Public Property IncCommand() As ICommand
    Public Property DecCommand() As ICommand

    Private count_ As Integer

  End Class

End Namespace

 もう一仕事、MainWindowViewModelのIncCommand/DecCommandと、CounterModelのIncrement/Decrementとを繋がにゃならんのですが、双方の型がICommandとAction<Object>なので変換アダプタ:DelegateCommandをC++/CLIで書きました。

DelegateCommand.h
#ifndef DELEGATECOMMAND_H__
#define DELEGATECOMMAND_H__

namespace DataBindingSample {

  public ref class DelegateCommand : public System::Windows::Input::ICommand {
  public:
    DelegateCommand(System::Action<Object^>^ execute);
    DelegateCommand(System::Action<Object^>^ execute, System::Func<Object^, bool>^ canExecute);

    virtual void Execute(Object^ parameter);
    virtual bool CanExecute(Object^ parameter);
    virtual event System::EventHandler^ CanExecuteChanged;

  private:
    System::Action<Object^>^     execute_;
    System::Func<Object^, bool>^ canExecute_;
    static bool alwaysTrue(Object^ arg);
  };

}

#endif
DelegateCommand.cpp
#include "DelegateCommand.h"

using namespace System;
using namespace System::Windows::Input;

namespace DataBindingSample {

  DelegateCommand::DelegateCommand(Action<Object^>^ execute) { 
    execute_ = execute;
    canExecute_ = gcnew Func<Object^,bool>(DelegateCommand::alwaysTrue);
  }

  DelegateCommand::DelegateCommand(Action<Object^>^ execute, Func<Object^, bool>^ canExecute) {
    execute_ = execute;
    canExecute_ = canExecute;
  }

  bool DelegateCommand::CanExecute(Object^ parameter) { return canExecute_(parameter); }
  void DelegateCommand::Execute(Object^ parameter) { execute_(parameter); }
  bool DelegateCommand::alwaysTrue(Object^ arg) { return true; }

}

次のページ
アプリケーションの完成

この記事は参考になりましたか?

特集記事連載記事一覧

もっと読む

この記事の著者

επιστημη(エピステーメー)

C++に首まで浸かったプログラマ。Microsoft MVP, Visual C++ (2004.01~2018.06) "だった"りわんくま同盟でたまにセッションスピーカやったり中国茶淹れてにわか茶...

※プロフィールは、執筆時点、または直近の記事の寄稿時点での内容です

この記事は参考になりましたか?

この記事をシェア

CodeZine(コードジン)
https://codezine.jp/article/detail/6539 2012/06/06 14:00

イベント

CodeZine編集部では、現場で活躍するデベロッパーをスターにするためのカンファレンス「Developers Summit」や、エンジニアの生きざまをブーストするためのイベント「Developers Boost」など、さまざまなカンファレンスを企画・運営しています。

新規会員登録無料のご案内

  • ・全ての過去記事が閲覧できます
  • ・会員限定メルマガを受信できます

メールバックナンバー