2011-05-14 35 views
0

工作這是我的MainPage.xaml中: -的ICommand沒有DataGrid中celltemplate

<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的到視圖模型實例。

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); 
      } 
     } 
    } 
} 

爲什麼我在ViewModel中的AddStudent觸發命令不起作用?任何想法?如果我把它放在Datagrid之外,它絕對沒問題。

回答

1

請看看this post

你需要一個DataContextProxy火一個DataGridCell內部命令。 ElementBinding不起作用。

+0

好吧,但我仍然無法理解爲什麼ElementBinding不會工作?你能回答嗎? – TCM 2011-05-15 06:58:30

相關問題