我正在为学校制作带MVVM Light的Windows 8.1应用程序。我面临的问题是使用Command属性。 我希望将单击的Movie对象发送到不同的页面并显示有关它的更多信息。 但是绑定失败:绑定不在我的DataContext中搜索,而是在我的Movie模型中搜索命令。 :(
错误讯息:
错误:BindingExpression路径错误:' ShowInfoMovieCommand'找不到' Howest.NMCT.RottenTomatoes.Models.Movie,Howest.NMCT.RottenTomatoes.Models,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'。 BindingExpression:Path =' ShowInfoMovieCommand' DataItem =' Howest.NMCT.RottenTomatoes.Models.Movie,Howest.NMCT.RottenTomatoes.Models,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#39 ;;目标元素是' Microsoft.Xaml.Interactions.Core.InvokeCommandAction' (名称='空&#39);目标财产是' Command' (键入' ICommand')
我视图中的我的XAML代码(数据上下文设置在页面顶部)
DataContext="{Binding GroupedItemsPageViewModel, Source={StaticResource Locator}}"
用于显示电影的ItemTemplate
<DataTemplate x:Key="RottenTomatoesMovieItemTemplate">
<Grid HorizontalAlignment="Left" Width="250" Height="230">
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Tapped">
<Core:InvokeCommandAction **Command="{Binding ShowInfoMovieCommand}" CommandParameter="{Binding id}"/>**
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
<Border>
<Image Source="{Binding posters.original}" Stretch="UniformToFill"/>
</Border>
<Grid VerticalAlignment="Bottom" Background="#B2CF4400" Height="50">
<Grid.RowDefinitions>
<RowDefinition Height="18*"/>
<RowDefinition Height="21*"/>
<RowDefinition Height="11*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="24*"/>
<ColumnDefinition Width="101*"/>
</Grid.ColumnDefinitions>
<TextBlock x:Name="TitleTextblock" Text="{Binding title}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Margin="15,0,0,0" Grid.ColumnSpan="2"/>
<Image Source="{Binding ratings.critics_rating, Converter={StaticResource RatingToPhotoConverter}}" Width="15" HorizontalAlignment="Left" Margin="15,0,0,0" Grid.Row="1"/>
<TextBlock Text="{Binding ratings, Converter={StaticResource RatingToPercentFormat}}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Grid.Column="1" Grid.Row="1" />
</Grid>
</Grid>
</DataTemplate>
我的页面ViewModel(GroupedItemsPageViewModel)
private void ShowInfoMovie(object clickedMovie)
{
Debug.WriteLine(clickedMovie);
}
private RelayCommand<object> _showInfoMovieCommand;
public RelayCommand<object> ShowInfoMovieCommand
{
get
{
if (_showInfoMovieCommand == null)
{
_showInfoMovieCommand = new RelayCommand<object>(
param => this.ShowInfoMovie(param)
);
}
return _showInfoMovieCommand;
}
}
答案 0 :(得分:1)
没关系自己找到它。 在我的datatemplate中,我的命令绑定应该是:
<Interactivity:Interaction.Behaviors>
<Core:EventTriggerBehavior EventName="Tapped">
<Core:InvokeCommandAction Command="{Binding DataContext.ShowInfoMovieCommand, ElementName=pageRoot}" CommandParameter="{Binding}"/>
</Core:EventTriggerBehavior>
</Interactivity:Interaction.Behaviors>
在我的ViewModel中,最好抓住我自己的模型(电影)
private void SetSelectedMovieDestinationView(Movie clickedMovie)
{
//Do Stuff
}