Чтобы уточнить ответ @ Энди.Это то, что я должен был сделать.
Добавить RelayCommand (эта реализация распространяется по Интернету, я полагаю, она исходит от MVVMLite):
using System;
using System.Windows.Input;
namespace WpfApp1
{
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
}
}
Определите мою собственную команду DeleteCommand вместо использования встроенной команды из DataGrid.Я сделал это в коде позади, но в моем реальном проекте это происходит в модели представления.
private RelayCommand<object> _DeleteCommand;
public RelayCommand<object> DeleteCommand => _DeleteCommand ?? (_DeleteCommand = new RelayCommand<object>((object o) => {
var proxy = o as Proxy;
proxyServers.Remove(proxy);
}, (object o) => {
if (o is Proxy)
{
return true;
}
else
{
return false;
}
}));
Я не мог использовать RelayCommand<Proxy>
, потому что новая строка элемента необъект прокси.Если бы у меня не было строки нового элемента, я мог бы использовать RelayCommand<Proxy>
.
Удалите это определение шаблона в XAML (потому что это ControlTemplate
, а не DataTemplate
, и кажется, что вам нужно DataTemplate
, если вы хотите передать элемент сетки команде какCommandParameter
)
<DataGridTemplateColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}" BasedOn="{StaticResource {x:Type DataGridCell}}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate >
<Button Command="{Binding DeleteCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:Proxy}}}"
IsEnabled="True" x:Name="deleteButton" Content="X">
</Button>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsEnabled" Value="True"/>
</Style>
</DataGridTemplateColumn.CellStyle>
Добавьте это вместо
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Command="{Binding DeleteCommand,RelativeSource={RelativeSource AncestorType={x:Type Window}}}"
CommandParameter="{Binding}"
IsEnabled="True" x:Name="deleteButton" Content="X">
</Button>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>