MediaPlaybackItem绑定到DataTemplate

MediaPlaybackItem绑定到DataTemplate

本文介绍了将MediaPlaybackItem绑定到DataTemplate中的ListView项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有以下listView:

Hello i have the following listView :

<ListView x:Name="SongList" HorizontalAlignment="Left" Visibility="Visible" Height="615" Background="White"
          Opacity="0.5" SelectedValue="selectedFile"
          ItemsSource="{Binding MediaModels.MediaPlaybackItemsList  , UpdateSourceTrigger=PropertyChanged}"
          Margin="986,105,0,0" VerticalAlignment="Top" Width="294">

所以在我的itemSource中,我绑定了一个MediaPlaybackItem列表.那一切都很好,除非我不知道如何将每个Mediaplayback项的艺术家和标题绑定到dataTemplate:

So in my itemSource i bind a list of MediaPlaybackItem .Thats fine everything works good except i dont know how to bind the artist and title for each mediaplayback item to the dataTemplate :

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

MediaPlaybacKItem类具有方法GetDisplayProperties().MusicProperties.Artist/Title/等.返回string.我只能通过此GetDisplayProperties方法访问这些properties,所以如何将其结果绑定到数据模板中?

THe MediaPlaybacKItem class has method GetDisplayProperties().MusicProperties.Artist/Title/etc. which returns string. I can access these properties only through this GetDisplayProperties method , so how can i bind the result of it in data Template ?

推荐答案

您不能将方法绑定到这样的属性.而是在您的MediaPlaybackItem中公开一个返回MusicProperties对象的属性.

You cannot bind a method to a property like that. Instead, expose a property in your MediaPlaybackItem that returns the MusicProperties object.

MediaPlaybackItem类

MediaPlaybackItem class

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

现在您可以将其绑定到您的ItemTemplate.

Now you can bind that to your 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>

这篇关于将MediaPlaybackItem绑定到DataTemplate中的ListView项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 22:17