문제

ListBox에 목록을 바인딩하려고합니다. 그리고 button1click 방법에서 myclass의 새로운 인스턴스가 내 목록 <>에 추가되지만 내 목록 상자에는 표시되지 않습니다. 거기 내 코드 :

       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",
                            });
}

내가 뭘 잘못 했어?

도움이 되었습니까?

해결책

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

보다

  1. ObservableCollection <(of <(t>)>) 클래스
  2. WPF에서 관측형 수집에 대한 소개
  3. Silverlight에서 List vs ObservableCollection vs InotifyPropertyChanged
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top