使用MVVM Light的Windows应用商店应用中的附加行为
我一直在尝试使用MVVM Light框架在一个简单的Windows应用商店应用程序中使用附加行为。因为我们不能使用System.Windows.Interactive,就像在Windows Phone中一样,我一直在使用和lybraries。它们工作得很好,但问题是,我不能在数据模板中声明附加的行为 我真正想要的是将命令附加到GridView中的任何项,以便我可以将项id作为参数传递。由于附加的行为不起作用,我找到的唯一解决方案是使用GridView的“SelectionChanged”事件,并将SelectedItem作为参数传递给ViewModel中的属性:使用MVVM Light的Windows应用商店应用中的附加行为,mvvm,windows-8,mvvm-light,datatemplate,attachedbehaviors,Mvvm,Windows 8,Mvvm Light,Datatemplate,Attachedbehaviors,我一直在尝试使用MVVM Light框架在一个简单的Windows应用商店应用程序中使用附加行为。因为我们不能使用System.Windows.Interactive,就像在Windows Phone中一样,我一直在使用和lybraries。它们工作得很好,但问题是,我不能在数据模板中声明附加的行为 我真正想要的是将命令附加到GridView中的任何项,以便我可以将项id作为参数传递。由于附加的行为不起作用,我找到的唯一解决方案是使用GridView的“SelectionChanged”事件,并
<GridView Grid.Row="1"
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemsGridView"
AutomationProperties.Name="Items"
TabIndex="1"
Padding="116,136,116,46"
ItemsSource="{Binding GeoTopArtists.topartists.artist}"
SelectionMode="Single"
SelectedItem="{Binding SelectedArtist, Mode=TwoWay}"
IsSwipeEnabled="False"
IsItemClickEnabled="False">
<WinRtBehaviors:Interaction.Behaviors>
<Win8nl_Behavior:EventToCommandBehavior Event="SelectionChanged" Command="SelectArtistCommand" CommandParameter="{Binding SelectedArtist.mbid}"/>
</WinRtBehaviors:Interaction.Behaviors>
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Image Source="{Binding image[4].text}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
</Border>
<StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding name}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="60" Margin="15,0,15,0"/>
<TextBlock Text="{Binding url}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>
这样做会很好(在ViewModel中不需要SelectedArtist属性)
这可能对您有所帮助。dependency属性已定义并连接到视图模型中的命令。因此,每当视图中有交互时,所选项目都可以作为命令参数发送,这在本视频中得到了清楚的演示
<GridView Grid.Row="1"
x:Name="itemGridView"
AutomationProperties.AutomationId="ItemsGridView"
AutomationProperties.Name="Items"
TabIndex="1"
Padding="116,136,116,46"
ItemsSource="{Binding GeoTopArtists.topartists.artist}"
SelectionMode="None"
IsSwipeEnabled="False"
IsItemClickEnabled="False">
<GridView.ItemTemplate>
<DataTemplate>
<Grid HorizontalAlignment="Left" Width="250" Height="250">
<Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
<Image Source="{Binding image[4].text}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}"/>
</Border>
<StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
<TextBlock Text="{Binding name}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="60" Margin="15,0,15,0"/>
<TextBlock Text="{Binding url}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
</StackPanel>
<WinRtBehaviors:Interaction.Behaviors>
<Win8nl_Behavior:EventToCommandBehavior Event="Tapped" Command="SelectArtistCommand" CommandParameter="{Binding Artist.mbid}"/>
</WinRtBehaviors:Interaction.Behaviors>
</Grid>
</DataTemplate>
</GridView.ItemTemplate>
</GridView>