0

我有一个 Wpf DataGrid,通过单击键盘上的删除键,我想在我的 ViewModel 中调用一个函数,DataGrid 绑定到 ViewModel 中的一个列表。代码如下所示:

数据网格:

<DataGrid  CanUserDeleteRows="False" ColumnWidth="*"  ItemsSource="{Binding MyViewModel.MyList}" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn  Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
            <DataGridTextColumn  Header="Value" Binding="{Binding Value}"></DataGridTextColumn>
        </DataGrid.Columns>
        <DataGrid.InputBindings>
            <KeyBinding Key="Delete" Command="{Binding DataContext.SomeCmd
                         RelativeSource={RelativeSource AncestorType=DataGrid}}" />
        </DataGrid.InputBindings>
</DataGrid>

此类的 DataGrid 的 DataContext 在其中包含 ViewModel

我的视图模型:

public class MyViewModel: INotifyPropertyChanged
{


    private IList<xx> myList= new List<xx>();
    public IList<xx> MyList
    {
        get
        {
            return myList;
        }
        set
        {
            myList= value;
            NotifyPropertyChanged("MyList");
        }
    }



    public void XM()
    {
       //DO SOMETHING
    }



    RelayCommand someCmd;
    public ICommand SomeCmd
    {
        get
        {
            if (someCmd== null)
            {
                someCmd= new RelayCommand(param => this.XM());
                NotifyPropertyChanged("SomeCmd");
            }
            return someCmd;
        }
    }

}

#region Relay Command
public class RelayCommand : ICommand
{
    #region Fields

    readonly Action<object> _execute;
    readonly Predicate<object> _canExecute;

    #endregion

    #region Constructors

    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");

        _execute = execute;
        _canExecute = canExecute;
    }
    #endregion

    #region ICommand Members


    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    #endregion

在 XX 类上有一个名称(字符串)和值(整数)

绑定到命令不起作用,并在 InitializeComponent() 上给出错误消息:

Object reference not set to an instance of an object.

如果我按以下方式编写命令的链接,则不会给出错误,但不会通过按删除来进入函数:

  <KeyBinding Key="Delete" Command="{Binding SomeCmd}" />
4

1 回答 1

1

XAML 应如下所示:

<DataGrid CanUserDeleteRows="False" ColumnWidth="*"  ItemsSource="{Binding MyList}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn  Header="Name" Binding="{Binding Name}"></DataGridTextColumn>
        <DataGridTextColumn  Header="Value" Binding="{Binding Value}"></DataGridTextColumn>
    </DataGrid.Columns>
    <DataGrid.InputBindings>
        <KeyBinding Key="Delete" Command="{Binding SomeCmd}" />
    </DataGrid.InputBindings>
</DataGrid>

不要忘记设置DataContext

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = new MyViewModel();
    }
}
于 2013-02-10T11:41:28.367 回答