简体   繁体   中英

Get CommandParameter from DataGrid Button

I have a simple question, I am having a button insile datagrid and it looks like:

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

I've managed to run command, but now i am having trouble passing command parameter, mainly id what am I doing wrong?

remove DataContext in CommandParameter:

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

Full xaml I used to test if I'm correct

 <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 and Command are:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM