WPF ListBox not bound to INotifyCollectionChanged or INotifyPropertyChanged events

I have the following test code:

private class SomeItem
    {
       public string Title{ get{ return "something"; } }
       public bool Completed { get { return false; } set { } }
    }

    private class SomeCollection : IEnumerable<SomeItem>, INotifyCollectionChanged
    {
        private IList<SomeItem> _items = new List<SomeItem>();
        public void Add(SomeItem item)
        {
            _items.Add(item);
            CollectionChanged(this, new 
             NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }

        #region IEnumerable<SomeItem> Members

        public IEnumerator<SomeItem> GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return _items.GetEnumerator();
        }

        #endregion

        #region INotifyCollectionChanged Members

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        #endregion
    }

    private SomeCollection collection = new SomeCollection();

    private void Expander_Expanded(object sender, RoutedEventArgs e)
    {
        var expander = (Expander) sender;
        var list = expander.DataContext as ITaskList;
        var listBox = (ListBox)expander.Content;
        //list.Tasks.CollectionChanged += CollectionChanged;
        collection.Add(new SomeItem());
        collection.Add(new SomeItem());
        listBox.ItemsSource = collection;
    }

and xaml

<ListBox Name="taskListList" ItemsSource="{Binding}" BorderThickness="0" ItemContainerStyle="{StaticResource noSelectedStyle}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
           <Expander Expanded="Expander_Expanded">
              <Expander.Header>
                <StackPanel Orientation="Horizontal">
                  <TextBlock Text="{Binding Name}" />
                  <TextBox KeyUp="TextBox_KeyUp" Width="200"/>
                  <Button Name="hide" Click="hide_Click">
                      <TextBlock Text="hide" />
                  </Button>
                </StackPanel>
               </Expander.Header>
                  <ListBox Name="taskList" ItemsSource="{Binding}" ItemTemplate=" 
                     {StaticResource taskItem}" />
              </Expander>
         </DataTemplate>
      </ListBox.ItemTemplate>
   </ListBox>

box . , ItemsSource (, , - , , , ). listbox , CollectionChanged . ICollection IEnumerable INotifyPropertyChanged, INotifyCollectionChanged INotifyPropertyChanged. , , - SomeCollection ObservableCollection<SomeItem>. , INotifyCollectionChanged ObservableCollection, , COM . //, INotify WPF.

, ( ).

+3
2

ObservableCollection<T> INotifyPropertyChanged. IEnumerable<T>, , ObservableCollection<T> create PropertyChanged Count Item[]. ObservableCollection<T>, IList<T> INotifyPropertyChanged. , .

0

, , WPF. ObservableCollection<SomeItem>, CollectionChanged .

private ObservableCollection<SomeItem> collection = 
    new ObservableCollection<SomeItem>();

, - SomeItem.PropertyChanged, SomeItem INotifyPropertyChanged

, CollectionChanged , ItemsSource, . , collection ListBox.ItemsSource. , ListBox.ItemsSource collection .

0

Source: https://habr.com/ru/post/1713424/


All Articles