2017-01-12 4 views
0

から、私は素朴な疑問を持って、私はデータグリッドinsileボタンを持っていますし、同じように見えます: は、Get CommandParameter DataGridのボタン

   <DataGrid.Columns> 
        <DataGridTextColumn Binding="{Binding name}" Header="Nazwa" /> 

        <DataGridTemplateColumn> 
         <DataGridTemplateColumn.CellTemplate> 
          <DataTemplate> 
           <Button Content="Usuń" CommandParameter="{Binding DataContext.Commendation.id, ElementName=dataGrid}" Command="{Binding DataContext.RemoveCommand, ElementName=dataGrid}" /> 
          </DataTemplate> 
         </DataGridTemplateColumn.CellTemplate> 
        </DataGridTemplateColumn> 
       </DataGrid.Columns> 

      </DataGrid> 

私はコマンドを実行するために管理してきましたが、今私はトラブルのコマンドを渡すを持っていますパラメータ、主に私は間違って何ですか?

答えて

1

CommandParameterでのDataContextを削除します。

public class ViewModelBase : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

public class DelegateCommand : ICommand 
{ 
    private readonly Action<object> execute; 
    private readonly Func<object, bool> canExecute; 
    public DelegateCommand(Action<object> execute, Func<object, bool> canExecute = null) 
    { 
     this.execute = execute; 
     this.canExecute = canExecute; 
    } 
    public bool CanExecute(object parameter) 
    { 
     return canExecute?.Invoke(parameter) ?? true; 
    } 

    public void Execute(object parameter) 
    { 
     this.execute?.Invoke(parameter); 
    } 

    public event EventHandler CanExecuteChanged; 
} 
public class ItemViewModel : ViewModelBase 
{ 
    private int idField; 

    public int id 
    { 
     get { return idField; } 
     set { idField = value; OnPropertyChanged();} 
    } 

    private string nameField; 

    public string name 
    { 
     get { return nameField; } 
     set { nameField = value; OnPropertyChanged(); } 
    } 

} 

public class MainWindowViewModel : ViewModelBase 
{ 
    public MainWindowViewModel() 
    { 
     this.RemoveCommand = new DelegateCommand(obj => 
     { 
      MessageBox.Show(obj.ToString()); 
     }); 
     this.Items = new ObservableCollection<ItemViewModel>() 
     { 
      new ItemViewModel() {id = 1, name = "some name1"}, 
      new ItemViewModel() {id = 2, name = "some name2"} 
     }; 
    } 
    public ICommand RemoveCommand { get; } 
    public ObservableCollection<ItemViewModel> Items { get; } 
} 
+0

はまだだけにチェックヌル –

+0

を渡してみました:私は、私が正しいんだ場合

<DataGrid ItemsSource="{Binding Items}" x:Name="dataGrid"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding name}" Header="Nazwa" /> <DataGridTemplateColumn> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <Button Content="Usuń" CommandParameter="{Binding id}" Command="{Binding DataContext.RemoveCommand, ElementName=dataGrid}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid> 

のviewmodelsとコマンドがあるテストするために使用

<Button Content="Usuń" CommandParameter="{Binding id}" Command="{Binding DataContext.RemoveCommand, ElementName=dataGrid}" />

完全なXAMLを。私のために働く –

関連する問題