2016-11-15 100 views
1

我是新來的WPF,並有問題綁定數據到一個簡單的列表框。我已經設定,在MainWindow.XAMLWPF列表框綁定沒有顯示

<ListBox Name="lbxShows" /> 

然後在後面的代碼,我設置ItemsSource屬性被稱爲ShowList顯示對象的一個​​ObservableCollection。這實際上是另一個類(Oasis)的一個屬性,其中OasisInst是一個實例(在MainApplication的構造函數中設置)。

InitializeComponent(); 
mainApp = new MainApplication(); 
lbxShows.ItemsSource = mainApp.OasisInst.ShowList; 

此時,ShowList中沒有項目,但稍後有些項目會被添加並且它們不會出現在ListBox中。

Oasis類的代碼實現INotifyPropertyChanged接口,然後具有從ShowList設置器調用的教科書方法。

private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

PropertyChanged是我的PropertyChangedEventHandler事件。

當我在調試模式下通過時,PropertyChanged爲null,所以似乎沒有訂閱我的事件。鑑於這通常會通過綁定自動發生(我認爲?),那麼我猜測綁定沒有正確設置。

也許單獨設置ItemsSource屬性不足以設置綁定?

+0

_「Oasis類的代碼實現INotifyPropertyChanged接口」_ - 只要您不更改「ShowList」屬性的值,這是無關緊要的。如果您要更改'ShowList'屬性的值,即創建一個全新的集合並將屬性值設置爲引用該集合,那麼僅僅實現該接口是不夠的。你需要將屬性綁定到目標'ItemsSource'屬性,而不是僅僅設置它。通常,這將在XAML中完成,但如果您堅持,也可以在代碼隱藏方面執行。 –

+1

你的問題是不應答的,因爲如果沒有好的[mcve],就無法確切知道代碼不起作用的原因。請解決你的問題。 –

回答

0

你在做什麼應該足以綁定ObservableCollection並讓U/I接收更新。 我認爲它不是那麼建議使用BindingObject,但發現它的作品。 (所以我也學到了一些東西)。 這是一個簡單的例子,應該與您提供的Xaml一起使用。它每秒鐘在列表中添加一個條目。

我很困惑,你提到「PropertyChanged是我的PropertyChangedEventHandler事件。」請注意,所需的唯一PropertyChangedEventHandler位於Oasis對象內部。

也許你的意思是你的U/I上有其他控件需要在你的MainWindow上有一個PropertyChangedEventHandler,但它不應該與Oasis對象中的PropertyChangedEventHandler交互。

----下面的代碼----

using System; 
using System.Collections.ObjectModel; 
using System.ComponentModel; 
using System.Runtime.CompilerServices; 
using System.Windows; 
using System.Windows.Threading; 

namespace ListBoxTest 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    /// 

    public class OasisInstance : INotifyPropertyChanged 
    { 
     public event PropertyChangedEventHandler PropertyChanged; 

     ObservableCollection<string> _items = new ObservableCollection<string>(); 
     public ObservableCollection<string> ShowList 
     { 
      get { return _items; } 
      set { 
       if (_items != value) 
       { 
        _items = value; NotifyPropertyChanged(); 
       } 
      } 
     } 

     private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") 
     { 
      if (PropertyChanged != null) 
      { 
       PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
      } 
     } 
    } 

    public class MainApplication 
    { 
     public OasisInstance OasisInst = new OasisInstance(); 
    } 

    public partial class MainWindow : Window 
    { 
     MainApplication mainApp = new MainApplication(); 
     DispatcherTimer timer = new DispatcherTimer(); 

     public MainWindow() 
     { 
      timer.Interval = TimeSpan.FromSeconds(1); 
      timer.Tick += (s, e) => { mainApp.OasisInst.ShowList.Add(DateTime.Now.ToString()); }; 
      timer.Start(); 

      InitializeComponent(); 

      lbxShows.ItemsSource = mainApp.OasisInst.ShowList; 
     } 
    } 
} 
+0

非常感謝您花時間回覆,這真的很有幫助。我現在已經開始工作了。有幾個問題。正如上面提到的,一件事是理解收集變化的引用的概念,而不是收集項目本身。另外,在我將ListBox的ItemsSource屬性設置爲ShowList的時候,我並沒有真正設置集合,所以ShowList爲null。雖然後來創建並填充了集合,但DataSource沒有提及它。我通過創建一個空集合,然後分配ItemSource,然後填充它來糾正它。 –

0

所有你在mainApp需要ShowList

public ObservableCollection<string> ShowList {get; set;} 

你必須有getters和setters`爲它工作。