繁体   English   中英

视图中的WPF按钮命令未触发到ViewModel中的ICommand

[英]WPF button command in view not firing to ICommand in viewmodel

我正在使用MVVM开发我的第一个应用程序,并且卡在命令按钮中。

我认为我有:

<Button x:Name="btSave" Command="{Binding AddContactCommand}" Content="Save" HorizontalAlignment="Left" Margin="183,186,0,0" VerticalAlignment="Top" Width="75"/>

在后面的代码中,我有:

public partial class ContactView : Window
{
    private ContactViewModel contactVM;

    public ContactView()
    {
        InitializeComponent();

        contactVM = new ContactViewModel();
        this.DataContext = contactVM;
    }
}

在视图模型中,我有:

public ICommand AddContactCommand { get; private set; }

void AddContact()
{
    ContactData ContactData = new ContactData();
    bSaved = ContactData.SaveContact("Theresa","Theresa","Theresa","Theresa");      
}

视图模型中的AddContact()永远不会被触发。

我做错了什么?

我认为这是问题所在,请检查您的DataContext。

如果您的ViewModel在构造函数后面的代码中,请输入以下内容:

DataContext = this;

否则,如果这是另一个类,则需要执行相同的操作,但要执行以下操作:

DataContext = new MyViewModel();

至少我认为这是您发布的问题。

当 itemSource 绑定到 Model (\

[英]How could one set the command property of a button in a View to an ICommand in the associated ViewModel when the itemSource is bound to a Model (\

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM