Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# wpfc中DataTemplate内部的Binding命令#_C#_Wpf_Data Binding - Fatal编程技术网

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}}}"/>