在尝试创建/添加此命令时,不确定我做错了什么:

时间:2015-10-05 11:38:05

标签: c# wpf mvvm

XAML按钮:

<Button Content="Test Connection" Name="btnTestConnection" Command="{Binding Path=TestCommand}" CommandParameter="{Binding ElementName=someObject}"/>

查看型号:

public ICommand TestCommand
{
    get;
    internal set;
}

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

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

public void TestExecute(object parameter)
{
    //do stuff with parameter

    obj.TestConnection(parameter);
}

我想指出在我的VM构造函数中调用CreateTestCommand()

最后,我实施了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;
   }
}

我在CreateTestCommand中设置了一个断点,看起来它配置正确:

enter image description here

但是当我点击btnTestConnection时,没有任何反应。我的视图模型中的TestExecute未被调用(在实际模型上调用TestConnection)。我一定是在遗漏一些东西,但我不能为我的生活找出什么......

编辑包括我的其他视图模型;

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);
    }

}

我遗漏了在第二个构造函数中设置的所有属性,因为它们实际上没有做任何事情,只是引用了模型对象上的相应值。

1 个答案:

答案 0 :(得分:0)

修改

查看模型

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

<Button Content="{Binding TestValue}" Command="{Binding Path=MyCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>

Xaml Code背后。

public MainWindow()
{
    InitializeComponent();
    this.DataContext = new ViewModel();
}