使用MVVM Light在Windows应用商店中附加行为

时间:2013-04-10 08:16:42

标签: mvvm windows-8 mvvm-light datatemplate attachedbehaviors

我一直在尝试使用MVVM Light框架在简单的Windows应用商店应用中使用附加行为。由于我们无法使用System.Windows.Interactivity,就像在Windows Phone中一样,我一直在使用Windows.UI.InteractivityWinRtBehaviors lybraries。它们运行良好,但问题是我无法在数据模板中声明附加行为

我真正想要的是将命令附加到GridView中的任何项目,以便我可以将项目ID作为参数传递。由于附加的行为不起作用,我找到的唯一解决方案是使用GridView的“SelectionChanged”事件并将SelectedItem作为参数传递给ViewModel中的属性:

    <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 属性)

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

1 个答案:

答案 0 :(得分:0)

link可能会帮助您。依赖项属性已定义并挂钩到视图模型中的命令。因此,只要视图中存在交互,就可以将所选项目作为命令参数发送,这在本视频中有明确说明。