Pregunta

Me tratando de unirse a la lista de cuadro de lista. Y en el método Button1Click nueva instancia de MyClass añade en mi lista de <>, pero que no es visible en mi cuadro de lista. Allí mi código:

       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;
                }
}

Es mi forma de episodio y 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",
                            });
}

¿Qué he hecho mal?

¿Fue útil?

Solución

Debe utilizar ObservableCollection<Node> en lugar de List<Node>. ObservableCollection es una colección de datos dinámico genérico que proporciona notificaciones (utilizando una interfaz "INotifyCollectionChanged") cuando se añaden elementos, eliminado, o cuando toda la colección se actualiza. Lista no implementa INotifyCollectionChanged, que es utilizado por la interfaz de WPF cuadro de lista para actualizar la interfaz de usuario.

ver

  1. ObservableCollection <(Of ) (>) Clase
  2. una introducción a ObservableCollection en WPF
  3. Lista vs vs ObservableCollection INotifyPropertyChanged en Silverlight
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top