我试图绑定列表,列表框。并在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列表框到更新的用户界面。

看看

  1. ObservableCollection<(的 <(T>)>)类
  2. 介绍ObservableCollection在WPF
  3. 表vs ObservableCollection vs举在Silverlight
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top