本文介绍了将 MediaPlaybackItem 数据绑定到 DataTemplate 中的 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 列表.那很好,一切正常,除了我不知道如何将每个媒体播放项目的艺术家和标题绑定到 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 项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 22:44