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