C# 不确定我';m尝试创建/添加此命令时出错:

C# 不确定我';m尝试创建/添加此命令时出错:,c#,wpf,mvvm,C#,Wpf,Mvvm,XAML按钮: <Button Content="Test Connection" Name="btnTestConnection" Command="{Binding Path=TestCommand}" CommandParameter="{Binding ElementName=someObject}"/> 我想指出,CreateTestCommand()是在我的VM构造函数中调用的 最后,我实现了TestCommand: class TestCommand : IComma

XAML按钮:

<Button Content="Test Connection" Name="btnTestConnection" Command="{Binding Path=TestCommand}" CommandParameter="{Binding ElementName=someObject}"/>
我想指出,
CreateTestCommand()
是在我的VM构造函数中调用的

最后,我实现了
TestCommand

class TestCommand : ICommand
{
   private Action<object> execute;

   private Predicate<object> canExecute;

   private event EventHandler CanExecuteChangedInternal;

   public TestCommand(Action<object> execute)
       : this(execute, DefaultCanExecute)
   {
   }

   public TestCommand(Action<object> execute, Predicate<object> canExecute)
   {
       if (execute == null)
       {
           throw new ArgumentNullException("execute");
       }

       if (canExecute == null)
       {
           throw new ArgumentNullException("canExecute");
       }

       this.execute = execute;
       this.canExecute = canExecute;
   }

   public event EventHandler CanExecuteChanged
   {
       add
       {
           CommandManager.RequerySuggested += value;
           this.CanExecuteChangedInternal += value;
       }

       remove
       {
           CommandManager.RequerySuggested -= value;
           this.CanExecuteChangedInternal -= value;
       }
   }

   public bool CanExecute(object parameter)
   {
       return this.canExecute != null && this.canExecute(parameter);
   }

   public void Execute(object parameter)
   {
       this.execute(parameter);
   }

   public void OnCanExecuteChanged()
   {
       EventHandler handler = this.CanExecuteChangedInternal;
       if (handler != null)
       {
           handler.Invoke(this, EventArgs.Empty);
       }
   }

   public void Destroy()
   {
       this.canExecute = _ => false;
       this.execute = _ => { return; };
   }

   private static bool DefaultCanExecute(object parameter)
   {
       return true;
   }
}
我忽略了在第二个构造函数中设置的所有属性,只是因为它们除了引用模型对象上的相应值之外什么都不做。

EDIT

视图模型

class ViewModel : INotifyPropertyChanged
{


    private string _test;

    public string TestValue
    {
        get { return _test; }
        set { _test = value; RaisePropertyChanged("TestValue"); }
    }

    public ICommand MyCommand { get; internal set; }


    public ViewModel()
    {
        TestValue = "Test";
        CreateTestCommand();
    }

    private void CreateTestCommand()
    {
        MyCommand = new TestCommand(ExecuteButton);
    }

    private void ExecuteButton(object obj)
    {
        TestValue = "Cool";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void RaisePropertyChanged(string propName)
    {
        var pc = PropertyChanged;
        if (pc != null)
        {
            pc(this, new PropertyChangedEventArgs(propName));
        }
    }
}
Xaml


我认为查看VM可能会有所帮助,这样我们可以查看绑定和datacontext。输出窗口是否显示任何绑定错误?@PScr编辑并添加了其余的内容VM@StillLearnin不,输出不显示任何内容。将XAML中的
命令
绑定到
公共ICommand
属性,但是在这里,您的
ICommand
属性名称与XAML绑定不匹配,您的方法名称可能也应该与
ICommand
属性不同。将
TestCommand
重命名为
TestCmd
根本不会改变行为;VM中的
TestExecute
不会触发,模型中的实际方法也不会触发。让我给出我使用的整个代码段。单击按钮时,使用属性已更改的代码实现,按钮文本为空。在我这一步,它可以完美地工作。不确定为什么按钮文本在您的末端变为空白
class FormProcessorViewModel
{
    FormProcessorModel obj;

    public FormProcessorViewModel()
    {
        obj = new FormProcessorModel();
        CreateTestCommand();
    }

    public FormProcessorViewModel(string server, string database, string username, bool specifyDateRange, DateTime startDate, DateTime endDate, string operation, string preprocessed, string processed, string failed) :this()
    {
        txtServer = server;
        txtDatabase = database;
        txtUsername = username;
        chkSpecifyDateRange = specifyDateRange;
        dpStartDate = startDate;
        dpEndDate = endDate;
        txtOperation = operation;
        txtPreprocessed = preprocessed;
        txtProcessed = processed;
        txtFailed = failed;
    }

    public ICommand TestCmd
    {
        get;
        internal set;
    }

    private bool CanExecuteTestCommand()
    {
        return !String.IsNullOrEmpty(txtUsername);
    }

    private void CreateTestCommand()
    {
        TestCmd = new TestCommand(TestExecute);
    }

    private void TestExecute(object parameter)
    {
        var passwordBox = parameter as PasswordBox;
        var password = passwordBox.Password;

        obj.TestConnection(password);
    }

}
class ViewModel : INotifyPropertyChanged
{


    private string _test;

    public string TestValue
    {
        get { return _test; }
        set { _test = value; RaisePropertyChanged("TestValue"); }
    }

    public ICommand MyCommand { get; internal set; }


    public ViewModel()
    {
        TestValue = "Test";
        CreateTestCommand();
    }

    private void CreateTestCommand()
    {
        MyCommand = new TestCommand(ExecuteButton);
    }

    private void ExecuteButton(object obj)
    {
        TestValue = "Cool";
    }

    public event PropertyChangedEventHandler PropertyChanged;

    void RaisePropertyChanged(string propName)
    {
        var pc = PropertyChanged;
        if (pc != null)
        {
            pc(this, new PropertyChangedEventArgs(propName));
        }
    }
}
<Button Content="{Binding TestValue}" Command="{Binding Path=MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
public MainWindow()
{
    InitializeComponent();
    this.DataContext = new ViewModel();
}