CollectionChanged event not registered when using Converter?
I have an ObservableCollection
called Collection1 that I want to bind to the ItemsSource
of a ListBox
through a converter.
When I specify a converter, the binding does not work -- it uses the converter for initialization only, and never again.
When I do not specify a converter, the binding works, but the user control does not display correctly because it does not understand the .
I learned that the CollectionChanged
event handler was not being set when the converter was specified, but was set when the converter was not specified. I do not know why this is the case.
To summarize:
This does not work:
<ListBox Name="theListBox"
Margin="8,28,8,8"
ItemsSource="{Binding Collection1, Converter={StaticResource myConverter}}"
ItemContainerStyle="{DynamicResource ContainerStyle}" />
Collection1.CollectionCha开发者_如何学JAVAnged is null.
This does work:
<ListBox Name="theListBox"
Margin="8,28,8,8"
ItemsSource="{Binding Collection1}"
ItemContainerStyle="{DynamicResource ContainerStyle}" />
Collection1.CollectionChanged is not null.
If anyone could help I'd appreciate it. Thanks!
Here was my solution to this problem, based on the other comments below.
Instead of binding through a converter, I created an ObservableCollection property in the class for binding and then manually subscribed to the Collection1.CollectionChanged event in the code.
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);
}
}
}
The XAML looks something like:
<ListBox x:Name="MyListBox"
Margin="8,28,8,8"
ItemContainerStyle="{DynamicResource ContainerStyle}"
ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=BindingCollection}" />
That seems to work just fine.
ItemsControl.ItemsSource has logic to connect to the CollectionChanged event and update its Items collection accordingly. Unless you're returning the same ObservableCollection instance from the converter, the CollectionChanged notifications have no way to propagate automatically from the Binding through the converter.
A precise fix would depend on what's happening in the converter.
UPDATE
Instead of converting the collection from one generic type to another, trying using the original ObservableCollection and instead change the converter to convert individual items and apply it to each by using it in the ItemTemplate of the control.
精彩评论