为什么不调用 RelayCommand?

Why isn't RelayCommand called?

我的 ViewModel 中有以下内容:

public MyViewModel() {
  CloseCommend = new RelayCommand(closeWindow);
}

public RelayCommand CloseCommend;
private void closeWindow() {
  Application.Current.MainWindow.Close();
}

XAML:

<Button ... Command="{Binding CloseCommend}"/>

我看到 ViewModel 构造函数已初始化,因此绑定应该存在。但是当我点击关闭按钮时,没有任何反应。知道我做错了什么吗?

从字段定义更改为 属性 定义:

public RelayCommand CloseCommand { get; set; }

为什么:

字段通常是不可绑定的。查看 Binding Sources Overview

You can bind to public properties, sub-properties, as well as indexers, of any common language runtime (CLR) object. The binding engine uses CLR reflection to get the values of the properties. Alternatively, objects that implement ICustomTypeDescriptor or have a registered TypeDescriptionProvider also work with the binding engine.

For more information about how to implement a class that can serve as a binding source, see Implementing a Class for the Binding Source later in this topic.

在“其他特征”部分下:

You cannot bind to public fields.