سؤال

أحاول ربط القائمة بـ 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>. ObservableCollection عبارة عن مجموعة بيانات ديناميكية عامة توفر إشعارات (باستخدام واجهة "INotifyCollectionChanged") عند إضافة العناصر أو إزالتها أو عند تحديث المجموعة بأكملها.القائمة لا تنفذ INotifyCollectionChanged, ، ما هي الواجهة التي يستخدمها WPF ListBox لتحديث واجهة المستخدم.

يرى

  1. ObservableCollection<(من <(T>)>) فئة
  2. مقدمة إلى ObservableCollection في WPF
  3. القائمة مقابل ObservableCollection مقابل INotifyPropertyChanged في Silverlight
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top