将MediaPlaybackItem数据绑定到DataTemplate中的ListView项

时间:2017-04-21 11:57:57

标签: mvvm data-binding binding uwp

您好我有以下listView:

<DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Border BorderBrush="#404040" BorderThickness="0,0,0,2" HorizontalAlignment="Stretch"
                                Width="282" Height="80">
                            <Grid HorizontalAlignment="Left">
                                <TextBlock Text="{Binding GetDisplayProperties().MusicProperties.Artist}" TextWrapping="WrapWholeWords"
                                           Margin="82,10,10,0" />
                                <Grid Margin="0,0,-140,0">
                                    <Image Source="Images/Album.png" Width="70" Height="70" Margin="0,0,350,0" />
                                </Grid>
                            </Grid>
                        </Border>
                    </StackPanel>
                </DataTemplate>

所以在我的itemSource中我绑定了一个MediaPlaybackItem的列表。很好,一切都很好,除了我不知道如何将每个媒体播放项目的艺术家和标题绑定到dataTemplate:

MediaPlaybacKItem

GetDisplayProperties().MusicProperties.Artist/Title/类有方法string等。返回properties。我只能通过此GetDisplayProperties方法访问这些locations,那么如何在数据模板中绑定它的结果呢?

1 个答案:

答案 0 :(得分:0)

您无法将方法绑定到类似的属性。相反,在MediaPlaybackItem中公开一个返回MusicProperties对象的属性。

MediaPlaybackItem类

public MusicPropertiesType MusicProperties
{
  get 
  {
    return this.GetDisplayProperties().MusicProperties;
  }
}

现在您可以将其绑定到ItemTemplate

XAML

<DataTemplate>
    <StackPanel Orientation="Horizontal">
        <Border BorderBrush="#404040" BorderThickness="0,0,0,2" HorizontalAlignment="Stretch"
                Width="282" Height="80">
            <Grid HorizontalAlignment="Left">
                <TextBlock Text="{Binding MusicProperties.Artist}" TextWrapping="WrapWholeWords"
                           Margin="82,10,10,0" />
                <Grid Margin="0,0,-140,0">
                    <Image Source="Images/Album.png" Width="70" Height="70" Margin="0,0,350,0" />
                </Grid>
            </Grid>
        </Border>
    </StackPanel>
</DataTemplate>