为什么我WPF结合不工作吗?
-
20-09-2019 - |
题
我试图绑定列表,列表框。并在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>
. ObservableCollection 是一个通用的动态数据收集提供通知的(使用一个接口"INotifyCollectionChanged
")当项目获得添加、删除或当整个集刷新。列表不会实现 INotifyCollectionChanged
, ,其接口使用由WPF列表框到更新的用户界面。
看看
不隶属于 StackOverflow