WPF DataGrid - 将dataitem的属性绑定为CommandParameter

时间:2014-07-14 08:20:43

标签: c# wpf xaml mvvm data-binding

我最近开始熟悉WPF / MVVM,但我遇到了绑定问题。我有一个Customer对象的ObservableCollection,我将它绑定到DataGrid。我想要实现的是将项目的ID属性绑定为Button命令的参数。

这里是CustomerViewModel.cs文件中的相关代码

private ObservableCollection<Customer> _customer = new ObservableCollection<Customer>();
public ObservableCollection<Customer> Customers
{
    get
    {
        return _customers;
    }
    set
    {
        SetProperty(ref _customers, value);
    }
}

private ICommand _openCustomerCommand;
public ICommand OpenCustomerCommand
{
    get
    {
        return _openCustomerCommand ?? (_openCustomerCommand = new RelayCommand(param => OpenCustomer((int)param)));
    }
}

和来自CustomerView.xaml的XAML:

<DataGrid ItemsSource="{Binding Customers}" >
  <DataGridTemplateColumn Header="Open" Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="Open" 
                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.OpenCustomerViewCommand}"
                    CommandParameter="{Binding Customers/ID}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>
</DataGrid>

我的问题如下:根据http://www.wpftutorial.net/BindingExpressions.html我可以使用正斜杠(/)来访问集合的当前项。但是,在这种情况下,接收的值为空。

我看到了Bind CommandParameter to DataItem for a Button in a DataGrid,建议使用CommandParameter = {Binding},但后来我收到绑定到该行的完整对象,这没关系,但不是我真正想要的。< / p>

2 个答案:

答案 0 :(得分:4)

如果您的媒体资源名称为ID,则只需使用以下

即可
<DataGrid ItemsSource="{Binding Customers}" >
  <DataGridTemplateColumn Header="Open" Width="Auto">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <Button Content="Open" 
                    Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.OpenCustomerViewCommand}"
                    CommandParameter="{Binding ID}" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
  </DataGridTemplateColumn>
</DataGrid>

答案 1 :(得分:2)

最终我弄明白了。尽管存在上述问题,但Nitin Joshi的建议在运行时编译并运行良好。

但是,当我在DataTemplate标记中定义集合的基础DataType时,我得到了我期望的行为(例如,具有Intellisense支持):

<Window x:Class="WPF_Demo.View.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:model="clr-namespace:WPF_Demo.Model"
        Title="MainWindow" Height="300" Width="300"
        DataContext="{StaticResource MainWindowViewModel}"
        >
    <Grid>
        <DataGrid ItemsSource="{Binding Customers}">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding ID}"></DataGridTextColumn>
                <DataGridTextColumn Binding="{Binding FullName}"></DataGridTextColumn>
                <DataGridTemplateColumn Header="Open" Width="Auto">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate DataType="model:Customer">
                            <Button Content="Open" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}, Path=DataContext.OpenCustomerViewCommand}" CommandParameter="{Binding ID}" />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>