WPF嵌套项目控件EventToCommand绑定(bind)路径错误

标签 wpf mvvm binding nested itemscontrol

我的项目控件中的 WPF 绑定(bind)有问题。出现以下错误

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

在这个 View 模型内部是一个 Relaycommand MovieImageClick,我希望将触发器连接到。

情况:
如果我将触发器直接放在 itemscontrol 中,它将起作用!如果我将触发器更深地放在 itemscontrol 内部,它就不起作用。所以我认为问题在于在整个项目控制中找到 View 模型。奇怪的是,触发器可以找到itemscontrol,但找不到命令什么的。请帮帮我!

最佳答案

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

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

关于WPF嵌套项目控件EventToCommand绑定(bind)路径错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29967370/

相关文章:

c# - WPF C# Properties.Settings 不存在

c# - 为什么 Setter 属性不适用于超链接?

c# - 有没有办法在序列化类的一部分时保留 XML 属性?

wpf - 了解 MVVM 中的模型和 View 模型

c# - 如何将绑定(bind)添加到数据网格中的列?

Silverlight 标记扩展

c# - 设置 Webview2 header 或 cookie

c# - 从 Xaml 运行异步方法

c# - Xamarin 表单绑定(bind)到对象属性

c# - Ninject + MVC3 没有注入(inject) Controller