【问题标题】:Unsure what I'm doing wrong when trying to create / add this command:不确定我在尝试创建/添加此命令时做错了什么:
【发布时间】:2015-10-05 11:38:04
【问题描述】:

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

我想指出,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;
   }
}

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

但是当我点击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);
    }

}

我省略了在第二个构造函数中设置的所有属性,只是因为它们除了引用模型对象上的相应值之外并没有真正做任何事情。

【问题讨论】:

  • 我认为查看VM可能会有所帮助,因此我们可以查看绑定和数据上下文。
  • 输出窗口是否显示任何绑定错误?
  • @PScr 编辑并添加了 VM 的其余部分
  • @StillLearnin 不,输出不显示任何内容。
  • 您将 XAML 中的 Command 绑定到 public ICommand 属性,但这里您的 ICommand 属性名称与 XAML 绑定不匹配,您可能应该将方法命名为不同于 @ 987654336@也有财产。

标签: c# wpf mvvm


【解决方案1】:

编辑

查看模型

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 代码。

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

【讨论】:

  • TestCommand 重命名为TestCmd 根本没有改变行为; VM 中的TestExecute 不会触发,模型中的实际方法也不会触发。
  • 让我给出我使用的整个sn-p。
  • 当我点击按钮文本时,它是空白的,使用您的代码和属性更改实现。
  • 它在我的末端完美运行。不知道为什么按钮文本最后会变成空白
猜你喜欢
  • 2011-05-15
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 2017-09-29
  • 2019-06-22
  • 1970-01-01
  • 1970-01-01
  • 2019-10-19
相关资源
最近更新 更多