c# - DataGrid 绑定中的 ListBox
问题描述
我在 XAML 中绑定有问题。我有一个 DataGrid,里面有一个 ListBox 按钮。我需要将按钮的命令参数绑定到 DataGrid 的项目 ID。我尝试使用RelativeSource,用Id命名隐藏列并尝试绑定列datacontext,但没有任何效果。我也尝试绑定到 DataGrid 中的 SelectedItem,然后命令可以正常工作,但 CanExecute 不能,因为每个按钮都根据选定的行进行更新,而不是它实际所在的行。这是我的代码:
看法:
<DataGrid
ItemsSource="{Binding Orders}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Order date" Binding="{Binding OrderDate}" />
<materialDesign:MaterialDataGridTextColumn Header="Status" Binding="{Binding Status}"/>
<materialDesign:MaterialDataGridTextColumn Header="Last update" Binding="{Binding LastUpdate}"/>
<materialDesign:MaterialDataGridTextColumn Header="Managed by" Binding="{Binding SomePerson}"/>
<materialDesign:MaterialDataGridTextColumn Header="Number" Binding="{Binding SomeNumber}"/>
<DataGridTemplateColumn>
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Button Content="{Binding ShowDetailsCommandViewModel.Name}" Command="{Binding ShowDetailsCommandViewModel.Command}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Actions">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ListBox ItemsSource="{Binding Actions}">
<ListBox.ItemTemplate>
<DataTemplate>
<Button Content="{Binding Name}" Command="{Binding Command}"
CommandParameter="THIS SHOULD BE BINDED TO ORDERVIEWMODEL.ID"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
视图模型:
public class MyOrdersViewModel
{
public MyOrdersViewModel()
{
}
public IList<OrderViewModel> Orders { get; set; }
}
public class OrderViewModel
{
public OrderViewModel()
{
}
public int Id { get; private set; }
public DateTime OrderDate { get; private set; }
public string Status { get; private set; }
public DateTime LastUpdate { get; private set; }
public string SomePerson { get; private set; }
public int SomeNumber { get; private set; }
public CommandViewModel ShowDetailsCommandViewModel { get; set; }
private bool showItems;
public bool ShowItems
{
get { return showItems; }
set
{
showItems = value;
RaisePropertyChanged();
}
}
public IList<CommandViewModel> Actions { get; private set; }
}
}
public class CommandViewModel
{
public CommandViewModel(string name, ICommand command)
{
Name = name;
Command = command;
}
public string Name { get; set; }
public ICommand Command { get; set; }
}
我需要将 OrderViewModel.Id 绑定到 OrderViewModel.Actions.Command 参数。我怎样才能做到这一点?
解决方案
使用 RelativeSource 从其 DataContext (OrderViewModel) 中查找父 DataGridRow 并绑定 Id:
CommandParameter="{Binding Path=DataContext.Id,
RelativeSource={RelativeSource AncestorType=DataGridRow}"/>
推荐阅读
- extjs - 使用构面优化 AEM QueryBuilder
- php - pdo按钮删除行
- sql - 防止 SQL 注入的动态 SQL 搜索
- git - 使用 git log -L 通过提交跟踪特定的代码行
- python - salt-ssh:尝试 cpmile sls 命令时出现错误
- javascript - 在 javascript 中重载 `?` 运算符以处理 null 或 undefined
- sql - 基于触发器的多列的row_number
- amazon-web-services - AWS Greengrass 核心在 lambda 函数中拥有自己的名称
- spring - Http 入站网关失败非法字符集
- rpm - RPM 覆盖文件(如果未修改)