WPF嵌套itemscontrol EventToCommand绑定路径错误

WPF nested itemscontrol EventToCommand binding path error

我在 itemscontrol 中的 WPF 绑定有问题。出现以下错误

System.Windows.Data Error: 40 : BindingExpression path error: 'MovieImageClick' property not found on 'object' ''RelativeSource' (HashCode=56697999)'. BindingExpression:Path=MovieImageClick; DataItem='RelativeSource' (HashCode=56697999); target element is 'EventToCommand' (HashCode=42916613); target property is 'Command' (type 'ICommand')

一个简单的例子XAML:

<ItemsControl x:Name="movie_poster_grid" ItemsSource="{Binding Mode=TwoWay, Path=AllMovies}">
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <WrapPanel Orientation="Horizontal"/>
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
   <ItemsControl.ItemTemplate>
      <DataTemplate>
         <Image Source="{Binding ImageURL}" Stretch="UniformToFill" Width="200" Height="300">
            <i:Interaction.Triggers>
               <i:EventTrigger EventName="MouseDown">
                  <cmd:EventToCommand Command="{Binding MovieImageClick , RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}}" PassEventArgsToCommand="True"/>
               </i:EventTrigger>
            </i:Interaction.Triggers>
         </Image>
      </DataTemplate>
   </ItemsControl.ItemTemplate>
</ItemsControl>

我有一个 MVVM 框架,并通过以下方式将数据上下文连接到 ModernUserControl:

DataContext="{Binding MovieListModel, Source={StaticResource Locator}}"

在这个视图模型中有一个 Relaycommand MovieImageClick 我希望将触发器连接到。

情况: 如果我将触发器直接放在 itemscontrol 中,它就会起作用!如果我将触发器放在 itemscontrol 的更深处,它就不起作用。所以我认为问题在于在整个项目控件中找到视图模型。奇怪的是trigger能找到itemscontrol却找不到command什么的。请帮帮我!

您正在绑定到 ItemsControl,但您需要 ItemsControl 的 DataContext。试试这个:

<cmd:EventToCommand Command="{Binding Path=DataContext.MovieImageClick, RelativeSource={RelativeSource FindAncestor, AncestorType=ItemsControl}}" PassEventArgsToCommand="True"/>