WPF DataGrid - 将数据项的属性绑定为 CommandParameter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24732248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF DataGrid - bind dataitem's property as CommandParameter
提问by Szeki
I recently started to familiarize myself with WPF / MVVM, but I got stuck with bindings. I have an ObservableCollection of Customer objects, I bind this to a DataGrid. What I want to achieve is to bind the item's ID property as a parameter of a Button's command.
我最近开始熟悉 WPF/MVVM,但我被绑定困住了。我有一个客户对象的 ObservableCollection,我将它绑定到 DataGrid。我想要实现的是将项目的 ID 属性绑定为按钮命令的参数。
Here's the relevant code from the CustomerViewModel.cs file
这是 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)));
}
}
and the XAML from CustomerView.xaml:
和来自 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>
My problem is the following: according to http://www.wpftutorial.net/BindingExpressions.htmlI can use the forward slash (/) to access the current item of the collection. However, in that case, the received value is null.
我的问题如下:根据http://www.wpftutorial.net/BindingExpressions.html我可以使用正斜杠 (/) 访问集合的当前项目。但是,在这种情况下,接收到的值为空。
I saw Bind CommandParameter to DataItem for a Button in a DataGrid, which suggests to use CommandParameter={Binding}, but then I receive the full object bound to the row, which is okay, but not really what I'm looking for.
我在 DataGrid 中看到Bind CommandParameter to DataItem for a Button,它建议使用 CommandParameter={Binding},但随后我收到了绑定到该行的完整对象,这很好,但并不是我真正想要的。
回答by Nitin Joshi
If you property name is IDthen just use following
如果您的属性名称是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>
回答by Szeki
Eventually I figured it out. Despite the mentioned issues, Nitin Joshi's suggestion compiles and works fine in runtime.
最终我想通了。尽管存在上述问题,Nitin Joshi 的建议在运行时编译并运行良好。
However, when I defined the underlying DataType of the collection in the DataTemplate tag, then I got the behavior I expected (e.g. having Intellisense support):
但是,当我在 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>

