0

所以我正在尝试创建自己的音乐播放器。我有一个使用当前播放列表正确更新的 Listview。当我想要一个网格来显示当前正在播放的歌曲时,我的问题就开始了。当绑定到这些字符串时,它们都返回Null.

从班级:

public class Song : INotifyPropertyChanged
{
    public string Title { get; set; }
    public string Path { get; set; }
    public string Artist { get; set; }
    public int Time { get; set; }
    public string Album { get; set; }
    //public ImageBrush Portrait { get; set; }
    public event PropertyChangedEventHandler PropertyChanged;


    public Song(string title, string path, string artist, int time, string album)//, ImageBrush portrait)
    {
        this.Path = path;
        this.Title = title;
        this.Artist = artist;
        this.Time = time;
        this.Album = album;
        //this.Portrait = portrait;
    }

    public string SongCurrentPlaying
    {
        get { return Title; }
        set
        {
            Title = value;
            OnPropertyChanged("SongCurrentPlaying");
        }
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(Title));
        }
    }

来自 XAML:

        <Grid HorizontalAlignment="Left" Height="143" VerticalAlignment="Top" Width="276">
            <Image HorizontalAlignment="Left" Height="124" Margin="10,10,0,0" VerticalAlignment="Top" Width="134" Stretch="Fill"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,100,0,0" TextWrapping="Wrap" Text="{Binding Songs.Title}" VerticalAlignment="Top" Height="25" Width="122"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,40,0,0" TextWrapping="Wrap" Text="{Binding SongCurrentPlaying.Title, PresentationTraceSources.TraceLevel=High}" VerticalAlignment="Top" Height="25" Width="122"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,70,0,0" TextWrapping="Wrap" Text="{Binding Song.Title}" VerticalAlignment="Top" Height="25" Width="122"/>
            <TextBlock HorizontalAlignment="Left" Margin="144,10,0,0" TextWrapping="Wrap" Text="{Binding Songs.Title}" VerticalAlignment="Top" Height="25" Width="122"/>
        </Grid>

如您所见,我一直在尝试不同的绑定以从 Title 中获取值,但没有一个成功。

4

2 回答 2

0

您的班级结构没有多大意义。您有一个Song包含 aSong信息的类。但是,该类也成立SongCurrentPlaying。现在,如果你的意思是和我SongCurentPlaying在同一个课程上,Song我会将你的问题缩小到你如何执行 XAML 绑定。我会假设DataContext您的控件的 尚未设置为Song. 例如

public partial class MainWindow : Window
{
    public MainWindow()
    {
        var someSong = new Song(...);
        this.DataContext = someSong;
        InitializeComponent();
    }
}

一旦设置为 ,DataContext您就可以直接绑定到类的属性(例如{Binding Title})。

于 2013-11-04T21:17:11.083 回答
0

您的歌曲类不需要实现INotifyPropertyChanged。您应该将您的歌曲视为模型。相反,您应该为您的音乐播放器制作一个视图模型,它将为您的视图保存所有信息,例如在这种情况下CurrentSongPlaying

我会做一个viewmodel看起来像这样的

internal class SongPlayerViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;

private Song _songPlaying;

public Song SongPlaying
{
  get { return _songPlaying; }
  set { 
    _songPlaying = value;
    OnPropertyChanged("SongPlaying");
  }
}



protected void OnPropertyChanged(string name)
{
  var handler = PropertyChanged;
  if (handler != null)
  {
    handler(this, new PropertyChangedEventArgs(name));
  }
}

}

然后确保datacontext在视图中将 设置为 this 的实例,在 xaml 内部只需将标题与这样的代码绑定即可。

数据上下文设置

private readonly SongPlayerViewModel _viewModel = new SongPlayerViewModel();

public MainWindow() 
{
   InitializeComponent();
   DataContext = _viewModel;
}

当前歌曲播放的示例 xaml 绑定

<TextBlock text="{Binding SongPlaying.Title}"/>

如果您在没有歌曲播放时想要一些东西,请查看Fallbackvalue

于 2013-11-04T21:26:43.793 回答