2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WPF MVVMLight ButtonのCommandにRelayCommandをバインドしたけど動かなかった時の対処メモ

Last updated at Posted at 2015-10-20

MVVM Lightを使ってWPFのアプリケーションを作っていたときのこと。

ButtonCommandViewModelRelayCommandをバインドして、ボタン押下の処理をViewModelで行おうとした。
しかしCanExecuteの処理が呼ばれない。そのためボタンの有効/無効が更新されない。そして押せない。困った。

結論

using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.CommandWpf;

usingの指定間違ってました。Wpfがついてる方使いましょう。
そもそもなぜ間違ったのか。それは「ctrl+.」でusingひっぱったらwpfなしの方が使われたから。気づかなくて一時間悩んだ…。

使用例としてはこんな感じです(細かい指定は省略)。
XAML
<Button Command="{Binding BtnCommand}" />
ViewModel
using GalaSoft.MvvmLight.CommandWpf;

~省略~

private RelayCommand _btnCommand;
public RelayCommand BtnCommand
{
    get
    {
        return _btnCommand ?? (_btnCommand = new RelayCommand(
            () =>
            {
            },
            () =>
            {
                /* ここが呼ばれなくて困った。*/
            }));
    }
}

…これにはまったの2回目…。なのでメモ。

2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?