WPF ListBox 未绑定(bind)到 INotifyCollectionChanged 或 INotifyPropertyChanged 事件

标签 wpf observablecollection inotifypropertychanged inotifycollectionchanged

我有以下测试代码:

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;
    }

和 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>

外部列表框在加载时填充。当扩展器被扩展时,我设置 ItemsSource内部列表框的属性(我这样做而不是使用绑定(bind)的原因是这个操作很慢,我只希望它在用户选择查看项目时发生)。内部列表框渲染得很好,但它实际上并没有订阅 CollectionChanged集合上的事件。我已经尝试过 ICollection而不是 IEnumerable并添加 INotifyPropertyChanged以及替换 INotifyCollectionChangedINotifyPropertyChanged .我真正可以让它工作的唯一方法是把我的SomeCollection类并继承自 ObservableCollection<SomeItem> .我尝试扮演自己的角色的理由INotifyCollectionChanged而不是使用 ObservableCollection是因为我在真实代码中包装了一个 COM 集合。该集合将通知添加/更改/删除,我正在尝试将这些转换为 INotify WPF 的事件。

希望这足够清楚(为时已晚)。

最佳答案

ObservableCollection<T>还实现了INotifyPropertyChanged .正如你收藏的只是一个 IEnumerable<T>您没有任何要为其创建事件的属性,但是 ObservableCollection<T>创建 PropertyChanged Count 的事件和 Item[]特性。你可以试着让你的收藏更像 ObservableCollection<T>源自 IList<T>并实现INotifyPropertyChanged .不过,我不知道这是否能解决您的问题。

关于WPF ListBox 未绑定(bind)到 INotifyCollectionChanged 或 INotifyPropertyChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1176319/

相关文章:

c# - 为什么 WPF 组合框项目源转换器获取整个集合而不是项目?

wpf - 如何从纹理图像 x、y 到 3d 空间进行 "reverse"纹理映射?

c# - 异步添加到 ObservableCollection(或替代方案)

wpf - 如何分配 INotifyPropertyChanged 的​​ PropertyChanged 事件?

wpf - 自定义 WPF slider ,图像为 Thumb

c# - TextBlock 选择性线条着色

wpf - 如何在 WPF 的列表框中显示对象的集合

c# - CollectionChanged 获取保存集合的对象的实例

c# - 更优雅地处理属性更改事件监听器(很多)(字典?)

c# - 如何调试未触发的 INPC 属性 setter ?