从DataGrid Button获取CommandParameter

时间:2017-01-12 18:16:25

标签: c# wpf mvvm

我有一个简单的问题,我有一个按钮insile 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>

我已经设法运行命令,但是现在我在传递命令参数方面遇到了麻烦,主要是id我做错了什么?

1 个答案:

答案 0 :(得分:3)

在CommandParameter中删除DataContext:

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

我用来测试我是否正确的完整xaml

 <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和Command是:

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