如何传递按钮的命令参数

本文关键字:命令 参数 按钮 何传递 | 更新日期: 2023-09-27 18:18:55

我想传递assignment_id这是一个命令参数的myButton .cs文件更新数据库中的值。我写了这段代码,但当我点击myButton时,它显示了一个消息框,方法和操作失败。我怎样才能解决这个问题?

private void Command(Int32 parameter)
{
    p = parameter;
}
private void btnUpdate_Click(Object sender, RoutedEventArgs e)
{  
    try
    {
        SqlConnection con = new SqlConnection("server=(local); Integrated Security=true; Database=nrcs");
        con.Open();
        SqlCommand comm = new SqlCommand("UPDATE Assignments SET assignment_title='myassignment'  WHERE assignment_id=" + p + ";", con);
        comm.ExecuteNonQuery();
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString());
    }
}

这里的Xaml代码:

<Button x:Name="myButton" Content="update" Command="{Binding Command}"     CommandParameter="{Binding assignment_id}" Click="btnUpdate_Click" ></Button>

如何传递按钮的命令参数

看起来您可能正在尝试混合命令和事件处理程序。如果您想要使用命令,您应该首先使用在那里定义的命令创建一个视图模型。然后将xaml中的数据上下文设置为该视图模型。然后,您应该能够以您想要的方式绑定命令。一个常见的方法是首先定义一个RelayCommand类。

一个简单的中继类可能看起来像这样:

public class RelayCommand<T> : ICommand
{
    #region Fields
    readonly Action<T> _execute = null;
    readonly Predicate<T> _canExecute = null;
    #endregion
    #region Constructors
    /// <summary>
    /// Initializes a new instance of <see cref="DelegateCommand{T}"/>.
    /// </summary>
    /// <param name="execute">Delegate to execute when Execute is called on the command.  This can be null to just hook up a CanExecute delegate.</param>
    /// <remarks><seealso cref="CanExecute"/> will always return true.</remarks>
    public RelayCommand(Action<T> execute)
        : this(execute, null)
    {
    }
    /// <summary>
    /// Creates a new command.
    /// </summary>
    /// <param name="execute">The execution logic.</param>
    /// <param name="canExecute">The execution status logic.</param>
    public RelayCommand(Action<T> execute, Predicate<T> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion
    #region ICommand Members
    ///<summary>
    ///Defines the method that determines whether the command can execute in its current state.
    ///</summary>
    ///<param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    ///<returns>
    ///true if this command can be executed; otherwise, false.
    ///</returns>
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute((T)parameter);
    }
    ///<summary>
    ///Occurs when changes occur that affect whether or not the command should execute.
    ///</summary>
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }
    ///<summary>
    ///Defines the method to be called when the command is invoked.
    ///</summary>
    ///<param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to <see langword="null" />.</param>
    public void Execute(object parameter)
    {
        _execute((T)parameter);
    }
    #endregion
}

这个例子来自于这个问题为什么RelayCommand

一旦你有了RelayCommand类,你就可以在ViewModel中定义命令,比如

 public class MainWindowViewModel
 {
       private ICommand _Command;
        public ICommand Command
        {
            get
            {
                if (_Command == null)
                    _Command = new RelayCommand<object>((x) => ExecuteCommand(x));
                return _Command;
            }
            set
            {
                _Command = value;
            }
        }
       private void ExecuteCommand(object parameter)
        {
            try
            {
                if (!string.IsNullOrEmpty(parameter.ToString()){
                  //Do sql call here. parameter.ToString() is a string representation of the parameter that was bound on the xaml
               }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
    }

现在您已经设置好了,只需将视图的数据上下文设置为视图模型。您可以在后面的代码中或在xaml中执行此操作,无论您想要什么。就像

    public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
现在你在xaml中对命令的绑定应该看起来像:
<Button x:Name="myButton" Content="update" Command="{Binding Command}"     CommandParameter="{Binding assignment_id}"></Button>

然而,当我们在这里,请改变你的sql命令使用参数,而不是像这样连接它作为一个字符串。如果你现在这样做,你就会被sql注入。

编辑:应该注意的是,您绑定到CommandParameter的对象也应该存在于您的视图模型中。理想情况下,你会有一个包含assignment_id属性的模型,然后你会在视图模型中实例化该模型的类型,然后将该模型的实例绑定到你的视图。

我认为这是点击事件(btnUpdate_Click)和命令被调用的顺序。如果首先调用事件,则变量'p'没有值,查询将失败。

我认为您应该使用事件或命令来执行查询,而不是两者都使用。如果您打算继续使用MVVM模式(我推荐),请在另一个类中使用该命令,该类将是您的视图模型。