Silverlight 4.0 ICommand不在datagrid celltemplate中工作

Silverlight 4.0 ICommand不在datagrid celltemplate中工作,silverlight-4.0,mvvm,Silverlight 4.0,Mvvm,这是我的主页。xaml:- <sdk:DataGrid Margin="17,17,20,76" AutoGenerateColumns="False" ItemsSource="{Binding Students}"> <sdk:DataGrid.Columns> <sdk:DataGridTextColumn Binding="{Binding StudName}" Header="Student Na

这是我的主页。xaml:-

  <sdk:DataGrid Margin="17,17,20,76" AutoGenerateColumns="False" ItemsSource="{Binding Students}">
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Binding="{Binding StudName}" Header="Student Name">

                </sdk:DataGridTextColumn>
                <sdk:DataGridTemplateColumn>
                    <sdk:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button CommandParameter="{Binding}"  Command="{Binding Path=DataContext.AddCommand,ElementName=root}"
                                    Content="Add Student" />
                        </DataTemplate>
                    </sdk:DataGridTemplateColumn.CellTemplate>
                </sdk:DataGridTemplateColumn>
            </sdk:DataGrid.Columns>

在代码隐藏中,我将datacontext设置为viewmodel实例

This is my viewmodel :-
using SampleApp.Misc;
using SampleApp.Model;
using SampleApp.Web;
using System.Collections.ObjectModel;
using SampleApp.Commands;

namespace SampleApp.VM
{
    public class MainPageViewModel : ViewModelBase
    {
        private StudentModel _model = new StudentModel();

        public MainPageViewModel()
        {
            _model.GetStudentAsyncComplete += _model_GetStudentAsyncComplete;
            _model.GetStudentAsync();
        }

        private RelayCommand<Student> _addCommand = null;

        public RelayCommand<Student> AddCommand
        {
            get
            {
                if (_addCommand == null)
                {
                    _addCommand = new RelayCommand<Student>(student =>
                    { 


                    }, student => student != null);
                }
                return _addCommand;
            }
        }

        private ObservableCollection<Student> _students;
        public ObservableCollection<Student> Students
        {
            get { return _students; }
            set
            {
                _students = value;
                RaisePropertyChanged("Students");
            }
        }

        void _model_GetStudentAsyncComplete(object sender, EntityResultArgs<Web.Student> e)
        {
            if (e.Error == null)
            {
                Students = new ObservableCollection<Student>(e.Results);
            }
        }
    }
}
这是我的视图模型:-
使用SampleApp.Misc;
使用SampleApp.Model;
使用SampleApp.Web;
使用System.Collections.ObjectModel;
使用SampleApp.Commands;
名称空间SampleApp.VM
{
公共类MainPageViewModel:ViewModelBase
{
private StudentModel _model=new StudentModel();
公共MainPageViewModel()
{
_model.GetStudentAsyncComplete+=\u model\u GetStudentAsyncComplete;
_model.GetStudentAsync();
}
private RelayCommand _addCommand=null;
公共RelayCommand AddCommand
{
得到
{
如果(_addCommand==null)
{
_addCommand=new RelayCommand(学生=>
{ 
},student=>student!=null);
}
返回_addCommand;
}
}
私立大学学生;
公选学生
{
获取{return\u students;}
设置
{
_学生=价值观;
RaisePropertyChanged(“学生”);
}
}
void\u model\u GetStudentAsyncComplete(对象发送方,EntityResultArgs e)
{
如果(e.Error==null)
{
学生=新的可观察集合(如结果);
}
}
}
}
为什么我的AddStudent命令没有在ViewModel中触发?有什么想法吗?如果我把它放在Datagrid之外,它绝对可以正常工作。

请查看


您需要DataContextProxy在DataGridCell内触发命令。ElementBinding不起作用。

好的,但我仍然无法理解ElementBinding为什么不起作用?你能回答吗?