所以我正在尝试创建自己的音乐播放器。
我有一个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获取值作为示例,但没有一个成功。
答案 0 :(得分:0)
你的班级结构没有多大意义。您有一个Song
课程,其中包含Song
个信息。但是,该课程也持有SongCurrentPlaying
。现在,如果您意味着与SongCurentPlaying
在同一个类上Song
,那么我会将您的问题缩小到如何执行XAML绑定。我假设您的控件的DataContext
尚未设置为Song
的实例。 e.g。
public partial class MainWindow : Window
{
public MainWindow()
{
var someSong = new Song(...);
this.DataContext = someSong;
InitializeComponent();
}
}
一旦设置为DataContext
,您就可以绑定到班级&#39;属性直接(例如{Binding Title}
)。
答案 1 :(得分: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
设置为此实例,并在xaml内部使用这样的代码绑定标题。
datacontext setup
private readonly SongPlayerViewModel _viewModel = new SongPlayerViewModel();
public MainWindow()
{
InitializeComponent();
DataContext = _viewModel;
}
当前歌曲播放的xaml绑定示例
<TextBlock text="{Binding SongPlaying.Title}"/>
如果您在没有播放歌曲时需要某些内容,请查看Fallbackvalue