C# wpfc中DataTemplate内部的Binding命令#
我的数据模板中有一个按钮标签。我想在单击按钮后触发该命令。如果按钮在数据模板之外,我的按钮可以工作。我尝试了以下几种解决方案:C# wpfc中DataTemplate内部的Binding命令#,c#,wpf,data-binding,C#,Wpf,Data Binding,我的数据模板中有一个按钮标签。我想在单击按钮后触发该命令。如果按钮在数据模板之外,我的按钮可以工作。我尝试了以下几种解决方案: <Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btnUpdate_Click" Command="{Binding Path=
<Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right" Click="btnUpdate_Click"
Command="{Binding Path=DataContext.updateProductionLineConfigCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}}"/>
这是我的命令类:
public ProductionLineConfigViewModel VM { get; set; }
public event EventHandler CanExecuteChanged;
public UpdateProductionLineConfigCommand(ProductionLineConfigViewModel vm)
{
VM = vm;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
VM.updateProductionLineConfig();
}
有人能帮我一下吗。为什么我无法将命令绑定到按钮?这是解决我问题的解决方案
<Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right"
Click="btnUpdate_Click" Command="{Binding DataContext.updateProductionLineConfigCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:production_line_config_home}}}"/>
您会遇到哪些错误?您是否在调试模式下运行此程序并查看输出窗口?在这里您可以看到绑定错误消息。没有显示错误消息。除非我删除DataContext并像这样键入命令=“{Binding Path=updateProductLineConfigCommand,RelativeSource={RelativeSource FindAncestor,AncestorType={x:type DataGrid}}}”
`然后我收到的消息是:System.Windows.Data错误:40:BindingExpression路径错误:“在对象”“DataGrid'(Name='productionLineConfigDataGrid')上找不到UpdateProductLineConfigCommand”属性。BindingExpression:path=updateProductionLineConfigCommand;DataItem='DataGrid'(Name='productionLineConfigDataGrid'));目标元素为“Button”(Name='btnUpdate');目标属性为“Command”(类型为“ICommand”)
@ChristianMurschall是的,我确信它的DataGrid。因此在DataGrid
的DataContext
中似乎找不到updateProductionLineConfigCommand
。也许配置跟踪级别有助于在输出窗口中查看更详细的绑定消息。请参阅
<Button x:Name="btnUpdate" Content="Update" VerticalAlignment="Center" HorizontalAlignment="Right"
Click="btnUpdate_Click" Command="{Binding DataContext.updateProductionLineConfigCommand,
RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type local:production_line_config_home}}}"/>