4

我想将作为 myButton 的命令参数的 assignment_id 传递给 .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>
4

2 回答 2

1

看起来您可能正在尝试混合使用命令和事件处理程序。如果你想使用命令,你应该首先创建一个视图模型,其中定义了命令。然后将 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
}

这个例子来自这个问题Why 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 属性的模型,然后您将在视图模型中实例化该模型的类型,然后将该模型的实例绑定到您的视图。

于 2019-12-08T19:00:09.973 回答
0

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

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

于 2013-06-18T09:27:22.720 回答