C# 使用转换器时是否未注册CollectionChanged事件?

C# 使用转换器时是否未注册CollectionChanged事件?,c#,wpf,observablecollection,C#,Wpf,Observablecollection,我有一个名为Collection1的ObservableCollection,我想通过转换器将其绑定到列表框的项源 当我指定转换器时,绑定不起作用——它只将转换器用于初始化,不再使用 当我没有指定转换器时,绑定可以工作,但是用户控件不能正确显示,因为它不理解 我了解到CollectionChanged事件处理程序不是在指定转换器时设置的,而是在未指定转换器时设置的。我不知道为什么会这样 总结如下: 这不起作用: Collection1.CollectionChanged为空。 这确实有效:

我有一个名为Collection1的
ObservableCollection
,我想通过转换器将其绑定到
列表框的
项源

当我指定转换器时,绑定不起作用——它只将转换器用于初始化,不再使用

当我没有指定转换器时,绑定可以工作,但是用户控件不能正确显示,因为它不理解

我了解到
CollectionChanged
事件处理程序不是在指定转换器时设置的,而是在未指定转换器时设置的。我不知道为什么会这样

总结如下:

这不起作用:


Collection1.CollectionChanged为空。
这确实有效:


Collection1.CollectionChanged不为空。
如果有人能帮忙,我将不胜感激。谢谢


根据下面的其他评论,我给出了这个问题的解决方案

我没有通过转换器进行绑定,而是在类中为绑定创建了ObservableCollection属性,然后在代码中手动订阅Collection1.CollectionChanged事件

public partial class MyScreen : UserControl
{
    public ObservableCollection<Class2> BindingCollection { get; set; }  // <-- Bind to this

    public MyScreen()
    {
        this.InitializeComponent();

        BindingCollection = new ObservableCollection<Class2>();

        Collection1.CollectionChanged += new NotifyCollectionChangedEventHandler(Collection1_CollectionChanged);
        MediViewData.Instance.ActivePatientCareReport.PropertyChanged += new PropertyChangedEventHandler(ActivePatientCareReport_PropertyChanged);

    }

    void Collection1_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        BindingCollection.Clear();

        foreach (var c1 in Collection1)
        {
            var c2 = ConvertClass1ToClass2(c1);
            if (c2 != null) BindingCollection.Add(c2);
        }
    }
}
public部分类MyScreen:UserControl
{

公共ObservableCollection BindingCollection{get;set;}//ItemsControl.ItemsSource具有连接到CollectionChanged事件并相应更新其Items集合的逻辑。除非您从转换器返回相同的ObservableCollection实例,否则CollectionChanged通知无法通过转换器从绑定自动传播

精确的修复取决于转换器中发生的情况

更新


与其将集合从一种泛型类型转换为另一种类型,不如尝试使用原始的ObservableCollection,并将转换器更改为转换单个项,并通过在控件的ItemTemplate中使用它将其应用于每个项。

我明白了,所以CollectionChanged必须在转换器返回的任何内容上注册?这就是有趣。它所做的只是将一种类型的集合转换为另一种类型的集合。@dythim:这是错误的做法。请事先将集合转换为
可观察集合
,并直接绑定到该集合。@Jon:谢谢。我将研究如何以这种方式重构它。
<ListBox Name="theListBox" 
         Margin="8,28,8,8"
         ItemsSource="{Binding Collection1}"
         ItemContainerStyle="{DynamicResource ContainerStyle}" /> 

Collection1.CollectionChanged is not null.
public partial class MyScreen : UserControl
{
    public ObservableCollection<Class2> BindingCollection { get; set; }  // <-- Bind to this

    public MyScreen()
    {
        this.InitializeComponent();

        BindingCollection = new ObservableCollection<Class2>();

        Collection1.CollectionChanged += new NotifyCollectionChangedEventHandler(Collection1_CollectionChanged);
        MediViewData.Instance.ActivePatientCareReport.PropertyChanged += new PropertyChangedEventHandler(ActivePatientCareReport_PropertyChanged);

    }

    void Collection1_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        BindingCollection.Clear();

        foreach (var c1 in Collection1)
        {
            var c2 = ConvertClass1ToClass2(c1);
            if (c2 != null) BindingCollection.Add(c2);
        }
    }
}
<ListBox x:Name="MyListBox" 
         Margin="8,28,8,8"
         ItemContainerStyle="{DynamicResource ContainerStyle}" 
         ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=BindingCollection}" />