首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从DataGrid获取CommandParameter按钮

从DataGrid获取CommandParameter按钮
EN

Stack Overflow用户
提问于 2017-01-13 02:16:25
回答 1查看 2.2K关注 0票数 0

我有一个简单的问题,我有一个按钮插入数据网格,它看起来像:

代码语言:javascript
复制
                <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,我做错了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-13 02:21:01

删除CommandParameter中的DataContext:

代码语言:javascript
复制
<Button Content="Usuń" CommandParameter="{Binding id}" Command="{Binding DataContext.RemoveCommand, ElementName=dataGrid}" />

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

代码语言:javascript
复制
 <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和命令是:

代码语言:javascript
复制
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; }
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41620205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档