2010-02-15 1 views
2

목록 상자에 목록을 바인딩하려고했습니다. Button1Click 메서드에서 MyClass의 새 인스턴스는 내 목록 <에 추가되지만 내 목록 상자에는 표시되지 않습니다. 이 내 코드 :왜 내 WPF 바인딩이 작동하지 않습니까?

 public static class NotesEngine 
      { 
       public static List<Note> All; 

       static NotesEngine() 
       { 
        All = new List<Note> 
           { 
            new Note 
             { 
              Content = "test1", 
             } 
           }; 
       } 

       public static List<Note> GetNotes() 
       { 
        return All; 
       } 
} 

그것은 내 양식 에피소드와 ObjectDataProvider입니다 :

내가 잘못 할 무엇
<ObjectDataProvider ObjectType="{x:Type NotesEngine}" x:Key="NotesList" MethodName="GetNotes"/> 

...... 

<TabItem Header="test" DataContext="{Binding Source={StaticResource NotesList}}"> 

       <ListBox HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
         ItemTemplate="{StaticResource NotesListBoxDataTemplate}" 
         ItemsSource="{Binding }"> 
       </ListBox> 
</TabItem> 

private void button2_Click(object sender, RoutedEventArgs e) 
{ 
    NotesEngine.All.Add(new Note 
          { 
           Content = "xx", 
           Images = new List<string>(), 
           LastEdit = DateTime.Now, 
           Title = "XASAC", 
          }); 
} 

?

답변

3

List<Node> 대신 ObservableCollection<Node>을 사용해야합니다. ObservableCollection은 항목을 추가, 제거 또는 전체 컬렉션을 새로 고칠 때 인터페이스 (INotifyCollectionChanged)를 사용하여 알림을 제공하는 일반 동적 데이터 수집입니다. 목록에는 UI를 업데이트하기 위해 WPF ListBox에서 사용하는 INotifyCollectionChanged이 구현되어 있지 않습니다.

  1. ObservableCollection<(Of <(T>)>) Class
  2. An Introduction to ObservableCollection in WPF
  3. List vs ObservableCollection vs INotifyPropertyChanged in Silverlight
참조