命令不是acssess绑定到ViewModel - wpf

时间:2013-02-10 09:49:44

标签: wpf mvvm icommand relaycommand

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

datagrid:

<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

我的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

On Class XX有一个名字(字符串)和值(int)

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

Object reference not set to an instance of an object.

如果我按以下方式编写命令的链接,则不会给出错误,但是不能通过删除来执行该功能:

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

1 个答案:

答案 0 :(得分: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();
    }
}