gpt4 book ai didi

c# - 为什么不调用 RelayCommand?

转载 作者:行者123 更新时间:2023-12-03 10:54:40 27 4
gpt4 key购买 nike

我的 ViewModel 中有以下内容:

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

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

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

我看到 ViewModel 构造函数已初始化,因此绑定(bind)应该在那里。但是当我点击关闭按钮时,什么也没有发生。任何想法我做错了什么?

最佳答案

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

public RelayCommand CloseCommand { get; set; }

为什么:

字段通常是不可绑定(bind)的。查看 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.

关于c# - 为什么不调用 RelayCommand?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39395646/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com