【问题标题】:Trouble Binding class to grid, second placeTrouble Binding class to grid,第二名
【发布时间】:2013-11-04 20:44:09
【问题描述】:

所以我正在尝试创建自己的音乐播放器。 我有一个使用当前播放列表正确更新的 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 中获取值,但没有一个成功。

【问题讨论】:

  • 它是否只为SongCurrentPlaying返回null
  • 是的。数据上下文为空

标签: c# wpf binding observablecollection


【解决方案1】:

您的班级结构没有多大意义。您有一个 Song 类,其中包含 Song 的信息。但是,该类也包含SongCurrentPlaying。现在,如果您意思SongCurentPlayingSong 放在同一个类上,那么我会将您的问题缩小到您如何执行XAML 绑定。我假设您的控件的DataContext 尚未设置为Song 的实例。例如

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

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

【讨论】:

  • 是的,我一直在努力解决这个问题。我的一个实验是在Song 中创建SongCurrentPlaying,也尝试过不同的类,但都失败了。您不能将其 datacontext 设置为 Song
【解决方案2】:

您的歌曲类不需要实现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

【讨论】:

  • 如何设置数据上下文?好像没办法。
  • 您以前从未设置过数据上下文吗?它就像听起来一样简单,用初始化数据上下文更新主帖子。
  • 但是数据上下文已经在公共主窗口中设置为 self。由于我的列表视图需要绑定到另一个类然后视图模型
猜你喜欢
  • 2022-08-22
  • 2020-04-05
  • 1970-01-01
  • 1970-01-01
  • 2022-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
相关资源
最近更新 更多