Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.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# 无法在列表框中绑定命令_C#_Wpf_Mvvm - Fatal编程技术网

C# 无法在列表框中绑定命令

C# 无法在列表框中绑定命令,c#,wpf,mvvm,C#,Wpf,Mvvm,我的WPF使用MVVM方法。我正在尝试绑定列表控件中的2个控件 <ListBox ItemsSource="{Binding ParentDuplicate}" SelectedItem="{Binding SelectedParent, UpdateSourceTrigger=PropertyChanged}"> <ListBox.ItemTemplate> <DataTemplate> <Stack

我的WPF使用MVVM方法。我正在尝试绑定列表控件中的2个控件

<ListBox ItemsSource="{Binding ParentDuplicate}" SelectedItem="{Binding SelectedParent, UpdateSourceTrigger=PropertyChanged}">
    <ListBox.ItemTemplate>
         <DataTemplate>
             <StackPanel>
                    <ContentControl Content="{Binding}" />
                    <Button Content="Delete me now" 
                            Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, Path=DeleteCommand}" 
                            CommandParameter="{Binding FilePath}" />
              </StackPanel>
           </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

但是上面的操作也会导致相同的绑定错误

祖先搜索会找到控件,而不是DataContext,因此您需要告诉绑定在哪里可以找到DeleteCommand属性。如果ViewModel是主窗口的DataContext,则可以使用:

<Button Content="Delete me now" 
    Command="{Binding RelativeSource={RelativeSource 
                  Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, 
                  Path=DataContext.DeleteCommand}" 
    CommandParameter="{Binding FilePath}" />

祖先搜索找到的是控件,而不是DataContext,因此您需要告诉绑定在哪里可以找到DeleteCommand属性。如果ViewModel是主窗口的DataContext,则可以使用:

<Button Content="Delete me now" 
    Command="{Binding RelativeSource={RelativeSource 
                  Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, 
                  Path=DataContext.DeleteCommand}" 
    CommandParameter="{Binding FilePath}" />

祖先搜索找到的是控件,而不是DataContext,因此您需要告诉绑定在哪里可以找到DeleteCommand属性。如果ViewModel是主窗口的DataContext,则可以使用:

<Button Content="Delete me now" 
    Command="{Binding RelativeSource={RelativeSource 
                  Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, 
                  Path=DataContext.DeleteCommand}" 
    CommandParameter="{Binding FilePath}" />

祖先搜索找到的是控件,而不是DataContext,因此您需要告诉绑定在哪里可以找到DeleteCommand属性。如果ViewModel是主窗口的DataContext,则可以使用:

<Button Content="Delete me now" 
    Command="{Binding RelativeSource={RelativeSource 
                  Mode=FindAncestor, AncestorLevel=1, AncestorType=Window}, 
                  Path=DataContext.DeleteCommand}" 
    CommandParameter="{Binding FilePath}" />


基本上,RelativeSource搜索可视化树,而您的DataContext/ViewModel不在其中。它是可视元素的一个属性。基本上,RelativeSource搜索可视树,而您的DataContext/ViewModel不在其中。它是可视元素的一个属性。基本上,RelativeSource搜索可视树,而您的DataContext/ViewModel不在其中。它是可视元素的一个属性。基本上,RelativeSource搜索可视树,而您的DataContext/ViewModel不在其中。它是一个可视元素的属性。我最终使用了命令=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType=Grid},Path=DataContext.DeleteCommand}”谢谢你,我最终使用了命令=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType=Grid}”,Path=DataContext.DeleteCommand}“谢谢你,我最后使用了Command=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType=Grid},Path=DataContext.DeleteCommand}”谢谢你,我最后使用了Command=“{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorLevel=1,AncestorType=Grid}”,Path=DataContext.DeleteCommand}“谢谢