4thSpace 4thSpace - 1 year ago 52
C# Question

Why isn't RelayCommand called?

I have the following in my ViewModel:

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

public RelayCommand CloseCommend;
private void closeWindow() {


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

I see the ViewModel constructor is initialized so the binding should be there. But when I click the close button, nothing happens. Any ideas what I'm doing wrong?

Answer Source

Change from a field definition to a property definition:

public RelayCommand CloseCommand { get; set; }


Fields are typically, not bindable. Check out the 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.

Under the "Other Characteristics" section:

You cannot bind to public fields.

Update your fiel

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download