Note that there are some explanatory texts on larger screens.

plurals
  1. POwpf attached property with collection doesnt get updated
    text
    copied!<p>I like to share a list between an application and a custom Usercontrol. I use an IEnumerable as attached property to provide a list to a Listbox inside the custom UserControl. The ListBox then receives the attached property as ItemsSource. This works so far. But when the host list changes, the list inside the usercontrol should get updated. How can I achieve this ? The current code sets the Usercontrol list, but when the host changes the list, the attached property won't get updated.</p> <p>The host that uses the UserControl has a ComboBox, which should share its ItemsSource with the UserControl's ListBox</p> <pre><code>public ObservableCollection&lt;Person&gt; PersonList { get; set; } </code></pre> <p>The host's Xaml binds a ComboBox to the collection:</p> <pre><code>&lt;ComboBox x:Name="combobox1" Width="200" ItemsSource="{Binding PersonList}" DisplayMemberPath="Name" SelectedIndex="0" IsEditable="True"&gt;&lt;/ComboBox&gt; </code></pre> <p>The Usercontrol which is placed inside the host receives the collection via attached property. The binding looks heavy but seems ok:</p> <pre><code>&lt;myUserCtrl:AdvEditBox ... prop:DynamicListProvider.DynamicList="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=DataContext.PersonList}"&gt; ... &lt;/myUserCtrl:AdvEditBox </code></pre> <p>The attached property has a callback, which currently gets called only once:</p> <pre><code>class DynamicListProvider : DependencyObject { public static readonly DependencyProperty DynamicListProperty = DependencyProperty.RegisterAttached( "DynamicList", typeof(IEnumerable), typeof(DynamicListProvider), new FrameworkPropertyMetadata(null, OnDynamicListPropertyChanged))); public static IEnumerable GetDynamicList(UIElement target) {..} public static void SetDynamicList(UIElement target, IEnumerable value) {..} private static void OnDynamicListPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e) { if (e.NewValue != null &amp;&amp; o is FrameworkElement) { ... } } </code></pre> <p>The OnDynamicListPropertyChanged() should be called whenever the PersonList of the host changes. Do I have to put INotifyCollectionChanged inside the attached property? If so, where and how ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload