【问题标题】:WPF DataGrid - bind dataitem's property as CommandParameterWPF DataGrid - 将数据项的属性绑定为 CommandParameter
【发布时间】:2014-09-04 02:47:53
【问题描述】:

我最近开始熟悉 WPF / MVVM,但我被绑定卡住了。我有一个客户对象的 ObservableCollection,我将它绑定到一个 DataGrid。我想要实现的是将项目的 ID 属性绑定为按钮命令的参数。

这是 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},但随后我收到了绑定到行的完整对象,这没关系,但并不是我真正想要的。

【问题讨论】:

    标签: c# wpf xaml mvvm data-binding


    【解决方案1】:

    如果您的属性名称是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>
    

    【讨论】:

    • 在这种情况下,我遇到了设计时错误,说“无法解析 'WPF_Demo.ViewModels.CustomerViewModel' 类型的数据上下文中的属性 'ID',可用的属性列表也未显示在智能感知
    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-05
      • 2017-05-16
      • 2012-10-12
      • 2012-04-02
      相关资源
      最近更新 更多