0

嗨,我是使用 MVVM 的新手,我正在尝试绑定列表框,但它不起作用。这是我的代码

模型

   public class Musicmodel : INotifyPropertyChanged
   {
    //variables privadas

      private String _artista;
      private Uri _href;
      private String _informacion;
      private Double _Dvalue;

    public String artista
    {
        get
         {
            return this._artista;  
          }
        set
        {
            this._artista= value;

            this.RaisePropertyChanged("artista");
        }
    }

    public Uri href { 
        get {

            return this._href;
         }

        set
        {
            this._href = value;
            this.RaisePropertyChanged("href");
        }

    }
    public String informacion {
        get 
        {
            return this._informacion;
        }

        set
        {
            this._informacion = value;
            this.RaisePropertyChanged("informacion");
        }
    }
    public Double Dvalue
    {
        get
        {
            return this._Dvalue;
        }
        set
        {
            this._Dvalue = value;
            this.RaisePropertyChanged("Dvalue");
        }

    }



    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
   }
  }

视图模型

public class DownloadFileViewModel : INotifyPropertyChanged
{

    private WebClient clienteDownload;


    private ObservableCollection<Model.Music>_musicSource= new ObservableCollection<Model.Music>();

    public ObservableCollection<Model.Music> musicSource
    {
        get
        {
            return this._musicSource;
        }
        set
        {
            this._musicSource = value;
            RaisePropertyChanged("musicSource");
        }
    }


    private int index = 0;


    //request para descargar la canción
    public void request(Model.Musicmodel item)
    {
        this.clienteDownload = new WebClient();
        this.clienteDownload.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clienteDownload_DownloadProgressChanged);

        //agregamos el item al music
        this.musicSource.Add(item);

        this.clienteDownload.OpenReadAsync(this.musicSource[index].href);

    }


    private void clienteDownload_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
       this.musicSource[index].Dvalue=(double)e.ProgressPercentage;

    }


    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }


  }
  }

看法

            <ListBox x:Name="list" ItemsSource="{Binding musicSource}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Vertical">
                        <TextBlock Text="{Binding artista}"/>
                        <ProgressBar Value="{Binding Dvalue}"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

代码背后

          protected override void OnNavigatedTo(NavigationEventArgs e)
          {

            DownloadFileViewModel download = new DownloadFileViewModel();
            Model.Music newMusic = new Model.Music() { href = new   Uri("http://media1.li.ru/b/4/mp3/2/95366/953662_14Friday_Im_In_Love.mp3", UriKind.Absolute), artista = "the cure" };
            download.request(newMusic);
            this.DataContext = download;
            base.OnNavigatedTo(e);

         }

我已经对此进行了调试,并且下载工作正常,并且我的 ObservableCollection 正确填充,没有任何问题,但是当我尝试绑定列表框时失败。请问我做错了什么?谢谢

4

2 回答 2

1

问题很简单。您在开始时初始化您的 musicSource 属性

private ObservableCollection<Model.Music>_musicSource= new ObservableCollection<Model.Music>();

然后在请求完成后添加东西。RaiseProperyChanged("Property")只会在您添加新的可观察集合时触发,但不会在您向其中添加项目时触发。

再次将此行添加到请求的末尾(当您填充 musicSource 时)

RaisePropertyChanged("musicSource");

这将触发视图中的另一个更新

编辑:

另一种方法是有一个额外的领域,如

private ObservableCollection<Model.Music>_anotherMusicSource= new ObservableCollection<Model.Music>();

并在上面做所有事情,然后说:

musicSource = _anotherMusicSource;

然后这将触发通知,一切都应该工作

于 2013-10-04T20:03:19.070 回答
1

您的属性名称中有下划线

private ObservableCollection<Model.Musicmodel> musicSource= new ObservableCollection<Model.Musicmodel>();

public ObservableCollection<Model.Musicmodel> _musicSource
{
    get
    {
        return this.musicSource;
    }
    set
    {
        this.musicSource = value;
        RaisePropertyChanged("musicSource");
    }
 }

你把这个搞混了——下划线应该(传统上)在私人成员上,而不是公众身上——你的绑定目标musicSource是私有的

.NET 提倡的标准约定是:

// Private member variables
private int _someInteger;

// Public ones
public int SomeInteger { get { ... } set { ... } }
于 2013-10-04T19:44:40.673 回答