Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将类绑定到网格时出现问题,第二名_C#_Wpf_Binding_Observablecollection - Fatal编程技术网

C# 将类绑定到网格时出现问题,第二名

C# 将类绑定到网格时出现问题,第二名,c#,wpf,binding,observablecollection,C#,Wpf,Binding,Observablecollection,所以我正试图创建我自己的音乐播放器。 我有一个列表视图正在使用当前播放列表正确更新。 当我想要一个网格来显示当前正在播放的歌曲时,我的问题就开始了。 当绑定到这些字符串时,它们都返回Null 来自课堂: public class Song : INotifyPropertyChanged { public string Title { get; set; } public string Path { get; set; } public string Artist { g

所以我正试图创建我自己的音乐播放器。 我有一个列表视图正在使用当前播放列表正确更新。 当我想要一个网格来显示当前正在播放的歌曲时,我的问题就开始了。 当绑定到这些字符串时,它们都返回
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中获取值作为示例,但都没有成功

你的班级结构没有多大意义。您有一个
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
,您就可以直接绑定到类的属性(例如
{Binding Title}
)。

您的歌曲类不需要实现
INotifyPropertyChanged
。你应该把你的歌曲想象成模特。相反,您应该为您的音乐播放器制作一个viewmodel,它将保存您查看的所有信息,例如本例中的
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内部使用如下代码绑定标题

数据上下文设置

private readonly SongPlayerViewModel _viewModel = new SongPlayerViewModel();

public MainWindow() 
{
   InitializeComponent();
   DataContext = _viewModel;
}
当前歌曲播放的xaml绑定示例

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


如果您在没有播放歌曲的情况下需要某些内容,请查看
Fallbackvalue

它是否只为
SongCurrentPlaying
返回
null
?是的。Datacontext为空是的,我一直在努力解决这个问题。我的一个实验是在
歌曲
中创建
歌曲当前播放
,也尝试了不同的类,但失败了。您不能将其设置为datacontext
Song
如何将datacontext设置为此?似乎无法。您以前从未设置过datacontext吗?这听起来很简单,我会用初始化的datacontext更新主帖。但是datacontext已经在公共主窗口中设置为self。由于我的listview需要绑定到另一个类,因此视图模型