¿Por qué mi WPF vinculante no trabajar?
-
20-09-2019 - |
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?
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
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow